使用Intent在活動之間穿梭
文章目錄
- 使用Intent在活動之間穿梭
- 前言
- 一、顯式Intent的使用
- 1.創建活動和布局
- 2.修改FirstActivity中按鈕的點擊事件,代碼如下所示:
- 3.代碼解釋
- 二、隱式Intent的使用
- 1.在活動的標簽下配置< intent-filter>的內容
- 2.修改FirstActivity中按鈕的點擊事件
- 3.代碼解釋
- 三、向下一個活動傳遞資料
- 1.FirstActivity的代碼:
- 2.SecondActivity的代碼:
- 3.代碼說明
- 4.Logcat截圖展示
- 四、回傳資料給上一個活動
- 1.FirstActivity的代碼:
- 2.SecondActivity的代碼:
- 3.Logcat截圖展示
- 總結
前言
一個應用中總會存在多個活動,通過這些活動的相互跳轉,資料傳遞,邏輯處理,便可完成一個應用的功能,在各個活動的跳轉時,會通過Intent(意圖),Intent使Activity跳轉大致可分為顯示意圖和隱式意圖.
一、顯式Intent的使用
1.創建活動和布局
①創建兩個活動FirstActivity和SecondActivity,都勾選Genarate Layout File選項但不勾選Launcher Activity選項,(如果有疑問或者不會創建活動的請參考前一課的文章)
②在FirstActivity的布局檔案中定義按鈕button1,在SecondActivity的布局檔案中中定義按鈕button2,
2.修改FirstActivity中按鈕的點擊事件,代碼如下所示:
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
});
3.代碼解釋
首先構建出了一個Intent,傳入FirstActivity.this作為背景關系,傳入SecondActivity.class作為目標活動,這樣我們的“意圖”就非常明顯了,即在FirstActivity這個活動的基礎上打開SecondActivity這個活動,然后通過
startActivity()方法來執行這個Intent,,這種傳入啟動活動和目標活動的方式稱為顯示Intent,不要忘記在AndroidManifest.xml檔案中添加Activity宣告,
二、隱式Intent的使用
相比較與顯示,隱式Intent則含蓄了很多,它并沒有明確指出我們想要啟動哪個活動,而是通過指定一系列更為抽象的action和category等資訊,然后由系統去分析這個Intent,并幫我們找到合適的活動去啟動,
1.在活動的標簽下配置< intent-filter>的內容
通過在標簽下配置的內容,可以指定當前活動能夠回應的action和category,打開AndroidManifest.xml,添加如下代碼:
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
2.修改FirstActivity中按鈕的點擊事件
修改FirstActivity中按鈕的點擊事件,代碼如下所示:
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.activitytest.ACTION_START");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
});
3.代碼解釋
①初始化Intent,為intent設定Action,添加Category,此時系統會自動幫助我們找到合適的需要啟動的活動,系統如何找到呢,這就回到了剛才我們在組態檔中設定的
< intent-filter>標簽的中的action和category了,如果同時滿足,則該活動就是系統為我們找到的將要啟動的最合適的活動了,其實每個活動在組態檔中宣告時,都會默認設定category引數,默認的值都為Intent.Category_Default,所以該屬性的設定通常使用時都省略設定了,
②簡單來說只有某個活動能夠同時匹配< action>和< category>時,才可以通過Intent來啟動它,
三、向下一個活動傳遞資料
當我們通過Intent在各個活動跳轉時,避免不了需要傳遞資料,在向目標活動傳遞資料時,不管是通過顯示還是隱式的方式傳遞時,系統提供了很多方便的Api供我們使用,
1.FirstActivity的代碼:
findViewById(R.id.button_1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("extra_data","Hello SecondActivity");
// 這個是為了防止你覺得一個一個的傳值很麻煩,可以傳遞一個bundle
Bundle bundle = new Bundle();
bundle.putString("bundle_string","sdsddfsf");
bundle.putInt("bundle_int",100);
intent.putExtra("bundle_data", bundle);
startActivity(intent);
}
});
2.SecondActivity的代碼:
public class SecondActivity extends AppCompatActivity {
private static final String TAG = "SecondActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
if (intent != null) {
if (intent.getStringExtra("extra_data") != null) {
String data = intent.getStringExtra("extra_data").toString();
Log.d(TAG, data);
}
Bundle bundle = intent.getBundleExtra("bundle_data");
if (bundle != null) {
if (bundle.get("bundle_string") != null && bundle.get("bundle_int") != null) {
String bundle_data = (String) bundle.get("bundle_string");
int intData = (int) bundle.get("bundle_int");
Log.d(TAG, bundle_data + "=" + intData);
}
}
}
3.代碼說明
①在啟動活動時傳遞資料的思路很簡單,Intent中提供了一系列putExtra()方法的多載,可以把我們想要傳遞的資料暫存在Intent中,啟動了另一個活動后,只需要把這些資料再從Intent中取出就可以了,
②putExtra()方法接收兩個引數,第一個引數是鍵,用于后面從Intent中取值,第二個引數才是真正要傳遞的資料,
③getIntent()方法獲取到用于啟動SecondActivity的Intent,然后呼叫getStringExtra()方法,傳入相應的鍵值,就可以得到傳遞的資料了,
④在上一章的內容中我們用到了方法onSaveInstanceState(),這個方法攜帶了一個Bundle引數,Bundle提供了一系列的方法用于保存資料,上面的代碼中putString()方法保護字串,putInt()方法保存整型資料,
⑤putExtra(“bundle_data”, bundle);中,bundle_data為鍵值對,第一個引數為鍵名,第二個引數為鍵對應的值,另一個活動的getBundleExtra("bundle_data")取出了Intent物件中的這些值,引數為鍵名,
4.Logcat截圖展示

四、回傳資料給上一個活動
1.FirstActivity的代碼:
findViewById(R.id.button_1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivityForResult(intent, 1);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
if (resultCode == RESULT_OK){
String return_data = data.getStringExtra("return_data");
if(return_data == null){
return;
}
Log.d(TAG, "回傳的資料:"+return_data);
}
break;
default:
break;
}
}
2.SecondActivity的代碼:
findViewById(R.id.button_2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("return_data", "Hello FirstActivity");
setResult(RESULT_OK, intent);
finish();
}
});
3.Logcat截圖展示

總結

您的關注與點贊,是對我的最大支持!
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/276216.html
標籤:其他
