本系列為《Android 第一行代碼第二版》讀書筆記
一、使用Intent在活動之間穿梭
上篇文章使用AS的Empty Activity體驗了Hello World,也就是說對于Android來說是Actiity疊加的,可以使用多個Activity,那么新建一個Empty 的Activity并創建其對應的layout檔案,
現在有兩個Activity分別為MainActivity、SecondActivity,現在需要完成的功能是在MainActivity上有一個按鈕,點擊這個按鈕可以進入到SecondActivity,SecondActivity上也有一個按鈕
上面的需求就是要進行兩個Activity之間的切換,那么很必然的在切換的同時,需要攜帶資料兩個Activity之間如何傳遞資料?那么就需要使用到Intent
1.1 顯式Intent
環境準備
first_layout.xml:定義一個Button_1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button 1"></Button>
</LinearLayout>
second_layout.xml:定義一個Button_3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".SecondActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_3"
android:text="Button 3"></Button>
</LinearLayout>
使用顯式的intent可以從一個Activity調到另外一個Activity
例如在MainActivity中定義按鈕點擊事件
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.first_layout);
Button button1 = (Button)this.findViewById(R.id.button_1);
button1.setOnClickListener((View view) -> {
//顯式Intent
Intent intent = new Intent(this, SecondActivity.class);
this.startActivity(intent);
});
}
}
public Intent(Context packageContext, Class<?> cls) {
mComponent = new ComponentName(packageContext, cls);
}
Intent函式接收兩個引數:
- 第一個引數Context為背景關系,一般使用this即可
- 第二個引數為要啟動的目標Activity
然后使用startActivity便可啟動該Activity
1.2 使用隱式Intent
使用隱式Intent則并不明確要啟動哪一個活動,而是在活動中指定一些action和category資訊,然后系統會去分析這個Intent,并找到合適的活動進行啟動
還是上面的例子,在AndroidManifest.xml定義一些資訊
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<!--默認的category -->
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY" />
</intent-filter>
</activity>
接著在MainActivity中
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.first_layout);
Button button1 = (Button)this.findViewById(R.id.button_1);
button1.setOnClickListener((View view) -> {
//使用隱式Intent
Intent intent = new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
});
}
}
action標簽指定當前活動可以回應com.example.activitytest.ACTION_START這個活動,而category標簽包含一些附加資訊,更精確的指明當前的活動能夠回應Intent中還可以帶有的category,只有在action和category中的內容同時匹配上Intent的內容的時候,該活動才可以回應Intent
1.3 向下一個活動傳遞資料
兩個Activity之間需要進行關聯需要Intent,所以傳遞資料也是使用Intent:
- Intent.putExtra(String name, @Nullable String value) :向Intent中寫入資料
- Intent.getxxxExtra:向Intent中取得資料
當然這些方法都是一些多載的方法,用于傳輸不同額資料
例如現在MainActivity需要向SecondActivity傳遞資料
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.first_layout);
Button button1 = (Button)this.findViewById(R.id.button_1);
button1.setOnClickListener((View view) -> {
//顯式Intent
String data = "Hello SecondActivity";
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data", data);
this.startActivity(intent);
});
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
//獲取資料
Intent intent = this.getIntent();
String data = intent.getStringExtra("data");
Log.d("Intent的資料" , data);
//通知
Toast.makeText(this,data, Toast.LENGTH_SHORT).show();
}
}
1.4 向上一個活動傳遞資料
既然可以向下傳遞一個資料,那么肯定可以向上傳遞資料,在Activity中有一個startActivityForResult方法用于啟動活動,但是這個方法期望在活動銷毀的時候能夠回傳一個結果給上一個活動,startActivityForResult方法接收兩個引數
- Intent:啟動的Activity
- requestCode:用于在回呼之后判斷資料的來源
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.first_layout);
Button button1 = (Button)this.findViewById(R.id.button_1);
button1.setOnClickListener((View view) -> {
Intent intent = new Intent(this, SecondActivity.class);
this.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 dataReturn = data.getStringExtra("data_return");
Toast.makeText(this, dataReturn, Toast.LENGTH_SHORT).show();
Log.d("向上傳遞資料", dataReturn);
}
break;
default:
}
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
//向上傳遞資料
Button button = (Button) this.findViewById(R.id.button_3);
button.setOnClickListener((View v) -> {
Intent intent = new Intent();
intent.putExtra("data_return", "Hello Return");
//這個方法很關鍵,用于向上傳遞資料,第一個引數為回傳處理結果一般使用RESULT_OK或者RESULT_CANCELED
this.setResult(RESULT_OK, intent);
//手動銷毀Activity
this.finish();
});
}
}
同時,在SecondActivity活動銷毀的時候還會去呼叫上一個活動的onActivityResult方法在這個方法中可以獲取到銷毀活動的資料,該方法引入三個引數:
- requestCode:就是startActivityForResult中傳入的int值
- resultCode:回傳結果
- data:銷毀的intent
同時,上面通過finish()方法來銷毀活動的,如果使用回退鍵也可以銷毀活動,所以可以在活動的生命周期函式上定義
SecondActivity.java
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("data_return", "Hello Return");
this.setResult(RESULT_OK, intent);
this.finish();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/282136.html
標籤:其他
