目錄
- Activity的創建
- 簡單跳轉案例
- 實作跳轉后回傳到原來的頁面
- Activity任務堆疊機制
- 實作跳轉后傳值
- 方法一
- 方法二:
- 實作Activity跳轉回傳值
- 補充
Activity的創建
如下操作

我們創建的activity命名為MainActivity2,后面簡稱為Activity2,默認的簡稱為Activity,然后會生成一個java檔案和一個xml布局,

所有的activity都一定要在manifests中注冊,剛剛創建的程序會自動注冊,

簡單跳轉案例
核心代碼
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
這里就不貼xml布局了,這個案例的組件只有一個button,用來實作簡單跳轉,
Activity
package com.hnucm.android_04_42;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 點擊按鈕跳轉到另外一個Activity
// 第一步:創建一個activity
// 跳轉到第二個activity
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
});
}
}
Activity
package com.hnucm.android_04_42;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
結果


默認的界面是哪一個?

實作跳轉后回傳到原來的頁面
修改Activity2
package com.hnucm.android_04_42;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 回傳到上一個acticity
//finish();
Intent intent=new Intent(MainActivity2.this,MainActivity.class);
startActivity(intent);
}
});
}
}

從上面的示例我們看出來,我們分別點了兩次跳轉和回傳,但是點擊左下角手機自帶的回傳點了五次才回到了桌面
這是為什么呢?
因為activity任務堆疊機制
Activity任務堆疊機制

每次使用Intent跳轉,都會在安卓的任務堆疊中產生一個新的Activity,壓入堆疊頂,而我們點擊右下角回傳時是不斷出堆疊的程序,才導致了上述的情況,而我們想要實作的是只啟動一個Activity和一個Activity2就可以了,
解決方法:finish方式把activity清除


實作跳轉后傳值
方法一
比之前的跳轉案例多了兩行代碼,xml布局中加入了兩個文本框
將兩個文本框中的值傳遞
Activity代碼
核心代碼
intent.putExtra("name",nameTv.getText().toString());
intent.putExtra("price",priceTv.getText().toString());
package com.hnucm.android_04_42;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
TextView nameTv=findViewById(R.id.textView);
TextView priceTv=findViewById(R.id.textView3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 點擊按鈕跳轉到另外一個Activity
// 第一步:創建一個activity
// 跳轉到第二個activity
// 頁面的值傳過去
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("name",nameTv.getText().toString());
intent.putExtra("price",priceTv.getText().toString());
startActivity(intent);
}
});
}
}
activity2代碼
核心代碼(接收值)
String name = getIntent().getStringExtra("name");
String price = getIntent().getStringExtra("price");
package com.hnucm.android_04_42;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//接收上一個頁面傳過來的值
String name = getIntent().getStringExtra("name");
String price = getIntent().getStringExtra("price");
TextView nameTv=findViewById(R.id.textView4);
TextView priceTv=findViewById(R.id.textView5);
nameTv.setText(name);
priceTv.setText(price);
Button button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 回傳到上一個acticity
finish();
//Intent intent=new Intent(MainActivity2.this,MainActivity.class);
//startActivity(intent);
}
});
}
}
跳轉前

跳轉后,原本的文本是默認的,跳轉后前一個頁面的值傳遞過來了

方法二:
當引數很多的話 把引數打包放到一個物件傳過去
新建一個Product類
package com.hnucm.android_04_42;
import java.io.Serializable;
public class Product implements Serializable {
private String name;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Activity代碼
核心代碼
Product product=new Product();
product.setName(nameTv.getText().toString());
product.setPrice(priceTv.getText().toString());
intent.putExtra("product",product);
package com.hnucm.android_04_42;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
TextView nameTv=findViewById(R.id.textView);
TextView priceTv=findViewById(R.id.textView3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: 點擊按鈕跳轉到另外一個Activity
// 第一步:創建一個activity
// 跳轉到第二個activity
// 頁面的值傳過去
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
// 當引數很多的話 把引數打包放到一個物件傳過去
Product product=new Product();
product.setName(nameTv.getText().toString());
product.setPrice(priceTv.getText().toString());
intent.putExtra("product",product);
//intent.putExtra("name",nameTv.getText().toString());
//intent.putExtra("price",priceTv.getText().toString());
startActivity(intent);
}
});
}
}
Activity2代碼
核心代碼
Product product = (Product) getIntent().getSerializableExtra("product");
nameTv.setText(product.getName());
priceTv.setText(product.getPrice());
package com.hnucm.android_04_42;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.Serializable;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//接收上一個頁面傳過來的值
//String name = getIntent().getStringExtra("name");
//String price = getIntent().getStringExtra("price");
Product product = (Product) getIntent().getSerializableExtra("product");
TextView nameTv=findViewById(R.id.textView4);
TextView priceTv=findViewById(R.id.textView5);
nameTv.setText(product.getName());
priceTv.setText(product.getPrice());
Button button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO 回傳到上一個acticity
finish();
//Intent intent=new Intent(MainActivity2.this,MainActivity.class);
//startActivity(intent);
}
});
}
}
實作Activity跳轉回傳值
在很多的時候我們需要將跳轉后的頁面修改的值回傳,那么我們要做的操作如下,
在剛剛跳轉的基礎上修改
Activity代碼
核心代碼
startActivityForResult(intent,0);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String s = data.getStringExtra("price");
priceTv.setText(s);
}

重寫方法

activity2代碼
在前一個案例的基礎上加了以下核心代碼
核心代碼
// 把值傳回去
Intent intent=getIntent();
intent.putExtra("price",priceTv.getText().toString());
setResult(1,intent);
finish();

activity_main代碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="iphone 12"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.478"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.159" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="268dp"
android:layout_marginEnd="156dp"
android:layout_marginRight="156dp"
android:text="跳轉"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="24dp"
android:text="6999"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main2代碼
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginLeft="168dp"
android:layout_marginTop="284dp"
android:text="第二個activity"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="88dp"
android:text="回傳"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintStart_toStartOf="@+id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginEnd="3dp"
android:layout_marginRight="3dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
補充
以下兩個都是生命周期函式

運行時出現順序

跳轉后回傳本頁面再次進入了onResume方法,
除了這兩個生命周期函式
還有onStart(),onRestart(),onPause(),onStop(),onDestory(),
執行順序如下

Android第一講筆記(常用控制元件以及線性布局)
Android第二講筆記(約束布局ConstraintLayout)
Android第三講筆記(Activity簡單操作)
歡迎批評指正…
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/272524.html
標籤:其他
上一篇:最全Android度量單位px dp(dip) ppi dpi sp pt的區別(螢屏尺寸,螢屏解析度,螢屏兼容,螢屏適配)
