简体   繁体   English

为什么在返回父活动时总是调用 onDestroy?

[英]Why is onDestroy always called when returning to parent activity?

I have a very simple app based on the Building Your First App tutorial.我有一个基于构建您的第一个应用程序教程的非常简单的应用程序 There are two activities: MainActivity invokes DisplayMessageActivity through startActivity() .有两个活动: MainActivity通过startActivity()调用DisplayMessageActivity

When entering DisplayMessageActivity , I see:输入DisplayMessageActivity ,我看到:

MainActivity.onStop()

as expected, but when I press the back button to return to the parent MainActivity , I get:正如预期的那样,但是当我按下后退按钮返回父MainActivity ,我得到:

MainActivity.onDestroy()
MainActivity.onCreate(null)
MainActivity.onStart()

The activity always gets destroyed for this very simple application.对于这个非常简单的应用程序,活动总是被破坏。 But according to the documentation (second bullet point), the typical behavior is for the activity to be stopped and restarted in such cases.但是根据文档(第二个要点),典型的行为是在这种情况下停止并重新启动活动。

Also, onDestroy() does not happen when first starting the child activity, but only once back button is clicked.此外,在首次启动子活动时不会发生onDestroy() ,而只会在单击后退按钮时发生。

Two questions:两个问题:

  1. Is there a way to prevent parent from being destroyed in the common case?有没有办法防止父级在普通情况下被破坏?
  2. Why is null being passed to onCreate() here?为什么null在这里被传递给onCreate() This prevents me from preserving state through onSaveInstanceState() .这阻止我通过onSaveInstanceState()保留状态。

Note that I've verified that Settings -> Developer Options -> Apps -> Don't keep activities is unchecked.请注意,我已验证设置 -> 开发人员选项 -> 应用程序 -> 不保留活动未选中。

Edit:编辑:

Here is how the child activity is linked to parent:以下是子活动与父活动的链接方式:

    <activity
        android:name="com.example.helloworld.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.helloworld.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.helloworld.MainActivity" />
    </activity>

Tracing through DisplayMessageActivity.onOptionsItemSelected() , I can see that it's calling Activity.onNavigateUp() .通过DisplayMessageActivity.onOptionsItemSelected()跟踪,我可以看到它正在调用Activity.onNavigateUp()

Thanks to Greg Giacovelli's comments, I found the answer here .感谢 Greg Giacovelli 的评论,我在这里找到了答案。 The solution was to set android:launchMode="singleTop" to the parent activity.解决方案是将android:launchMode="singleTop"设置为父活动。

I still can't understand why such basic information is so unknown and hard to find!我还是不明白为什么这样的基本信息如此未知和难以找到!

Adding launchMode in Manifest changes launch mode every time, even it is not launched by child activity.在 Manifest 中添加 launchMode 每次都会更改启动模式,即使它不是由子活动启动的。 There are other ways to launch existed instance.还有其他方法可以启动现有实例。

1.override onOptionsItemSelected(item: MenuItem) 1.override onOptionsItemSelected(item: MenuItem)

override fun onOptionsItemSelected(item: MenuItem): Boolean {
   return when (item.itemId) {
        android.R.id.home -> {
            // Respond to the action bar's Up/Home button
            val upIntent: Intent? = NavUtils.getParentActivityIntent(this)
            when {
                upIntent == null -> throw IllegalStateException("No Parent Activity Intent")
                else -> {
                    //add launch flag here
                    upIntent.flags=Intent.FLAG_ACTIVITY_CLEAR_TOP
                    NavUtils.navigateUpTo(this, upIntent)
                }
            }
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
}
  1. 2nd solution looks like a bug.第二个解决方案看起来像一个错误。 Set "android:parentActivityName" to activity which is not existed in current task stack.将“android:parentActivityName”设置为当前任务堆栈中不存在的活动。 Android will behavior like "BACK" button. Android 的行为类似于“返回”按钮。 It launches the top instance instead of launching parent activity.它启动顶部实例而不是启动父活动。 Document mentions "NavUtils.shouldUpRecreateTask" can recognize if parent activity existed in current task.文档提到“NavUtils.shouldUpRecreateTask”可以识别当前任务中是否存在父活动。 But it doesn't work in my test.但它在我的测试中不起作用。 Navigate up with a new back stack 使用新的返回堆栈向上导航

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM