MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction("android.intent.action.ysdy" );
startActivityForResult(intent,2);
}
});
Button button2=findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this,ThirdActivity.class);
Bundle bundle=new Bundle();
bundle.putString("name","张三");bundle.putInt("age",20);
intent.putExtra("data",bundle);
intent.putExtra("data2","未捆绑的数据");
startActivityForResult(intent,3);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "从第"+requestCode+"个Activity返回。", Toast.LENGTH_LONG).show();
if(resultCode==3){
String string=data.getStringExtra("hello");
Toast.makeText(this, string, Toast.LENGTH_LONG).show();
}
}
}
SecondActivity
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
Log.i("wutest", "第2个Activity准备销毁...: ");
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("wutest", "第2个Activity销毁了。 ");
}
}
ThirdActivity
public class ThirdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
String receiver="接收的数据如下:\n";
Intent intent=getIntent();
Bundle data = intent.getBundleExtra("data");
receiver+="name:"+data.getString("name")+"\n";
receiver+="age:"+data.getInt("age");
receiver+="\n"+intent.getStringExtra("data2");
Toast.makeText(this, receiver, Toast.LENGTH_LONG).show();
intent=new Intent();
intent.putExtra("hello","How are you?");
setResult(3,intent);
}
}
activity_main.xml
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="107dp"
android:layout_marginTop="63dp"
android:text="调用第2个Activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="46dp"
android:text="调用第3个Activity"
app:layout_constraintStart_toStartOf="@+id/button1"
app:layout_constraintTop_toBottomOf="@+id/button1" />
activity_second.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是被调用的Activity."
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="124dp"
android:layout_marginTop="148dp"
android:text="主动销毁当前活动"
app:layout_constraintStart_toStartOf="@+id/textView1"
app:layout_constraintTop_toTopOf="@+id/textView1" />
activity_third.xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3个Activity"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
清单文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ThirdActivity"></activity>
<activity
android:name=".SecondActivity"
android:label="隐式调用SecondActivity">
<intent-filter>
<action android:name="android.intent.action.ysdy" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>