按下返回按钮并调用finish()应该具有完全相同的行为,前提是您没有覆盖onBackPressed。
当从Activity A启动Activity B时,将发生以下事件:
- Activity A将被暂停(只要有足够的资源可用,就不会被销毁)
- Activity B将被创建并恢复
在从Activity B调用finish()或按下返回按钮时,将发生以下事件:
- Activity B将被暂停
- Activity A将被恢复
- Activity B将被销毁
Activity A不会再次创建,而是恢复。 (除非它因释放资源而被销毁)。
您可以使用以下示例应用程序验证相同的内容。
MainActivity.java
package com.example.activitytest;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "MainActivity created", Toast.LENGTH_SHORT).show();
Button launchChild = (Button) findViewById(R.id.btnLaunchChild);
launchChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(getBaseContext(), ChildActivity.class);
startActivity(myIntent);
}
});
}
@Override
protected void onResume(){
super.onResume();
Toast.makeText(getApplicationContext(), "MainActivity resumed", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause(){
super.onPause();
Toast.makeText(getApplicationContext(), "MainActivity paused", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy(){
super.onPause();
Toast.makeText(getApplicationContext(), "MainActivity destroyed", Toast.LENGTH_SHORT).show();
}
}
ChildActivity.java
package com.example.activitytest;
public class ChildActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Toast.makeText(getApplicationContext(), "Child created", Toast.LENGTH_SHORT).show();
Button finish = (Button) findViewById(R.id.btnFinish);
finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
@Override
protected void onResume(){
super.onResume();
Toast.makeText(getApplicationContext(), "Child resumed", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause(){
super.onPause();
Toast.makeText(getApplicationContext(), "Child paused", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy(){
super.onPause();
Toast.makeText(getApplicationContext(), "Child destroyed", Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btnLaunchChild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/launch_child" />
</RelativeLayout>
activity_child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnFinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/finish" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.activitytest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.activitytest.ChildActivity">
</activity>
</application>
</manifest>
onBackPressed
方法? - Shobhit PurionActivityResult
。 - vlio20onKeyDown()
方法吗? - nKn