[英]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:两个问题:
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.