子活动返回按钮与finish()函数的区别

3
我注意到这个问题:我有一个父活动A,打开子活动B,在活动B中,如果我以某种方式使用finish()关闭此活动,则父活动会重新加载并返回初始状态,但是如果我点击后退键,则会返回到活动A,就像我离开时的状态一样。
以下是我结束活动B的示例:
@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
   switch (item.getItemId()) 
   {
     case android.R.id.icon:
        finish();
        return true;
     default:
        return super.onOptionsItemSelected(item);
   }
}

以下是我如何从A活动打开B活动的方法:

Intent intent = new Intent(thisActivity, toActivity);
startActivity(intent);

这里是清单XML:

<activity
    android:name="com.evapp.activities.A"
    android:label="@string/A" >
</activity>
<activity
    android:name="com.evapp.activities.B"
    android:configChanges="orientation"
    android:label="@string/B"
    android:parentActivityName="com.evapp.activities.A"
    android:screenOrientation="portrait" >

    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.evapp.activities.A" />
</activity>

有人能解释一下finish()和return之间的区别吗?

你是否重写了 onBackPressed 方法? - Shobhit Puri
最好能够达到相同的效果。你能展示一下你完成它的地方以及相关的代码吗? - Shobhit Puri
是的 @Shobhit Puri,我已经更新了问题。 - vlio20
我没有覆盖 onActivityResult - vlio20
你覆盖了 onKeyDown() 方法吗? - nKn
显示剩余5条评论
2个回答

1
按下返回按钮并调用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);
              //myIntent.putExtra("key", value); //Optional parameters
              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>

1

当使用片段时,调用finish()并按下返回按钮并不完全相同。如果我们检查Android Activity的源代码,我们可以看到它不仅仅是立即调用finish(),而是在此之前调用其FragmentManagerpopBackStackImmediate()方法。

/**
 * Called when the activity has detected the user's press of the back
 * key.  The default implementation simply finishes the current activity,
 * but you can override this to do whatever you want.
 */
public void onBackPressed() {
    if (!mFragments.popBackStackImmediate()) {
        finish();
    }
}
popBackState() 的作用是从后退栈中移除最新的片段。Immediate 意味着它会立即执行,而不是异步执行。当没有片段被移除时,它才会返回 false - 在这种情况下,将调用 finish()
我不确定为什么在调用 finish() 时您的第一个活动似乎会重新初始化。如果您确实回到了第一个活动,则应该执行其 onResume() 方法,这可能会改变其外观。要完全理解发生了什么,我们应该看看您如何创建片段以及 onResume() 方法中发生了什么。

我目前还没有使用 onResume,但我会添加打开 B 活动的代码。 - vlio20

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接