Android的parentActivity不startActivityForResult返回后得到重建 [英] Android parentActivity not getting recreated after startActivityForResult returns

查看:371
本文介绍了Android的parentActivity不startActivityForResult返回后得到重建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MainActivity和里面,我加载一个片段A.从碎裂,我打电话用startActivityforResult如下谷歌placepicker活动。

I have a MainActivity and inside it, I am loading a fragment A. From FragmentA , I am calling google placepicker activity using startActivityforResult as follows.

PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
Intent intent = builder.build(getActivity());
getActivity().startActivityForResult(intent,PLACE_PICKER_REQUEST);

但是,当我选择的地方,onActivityResult(无论是在碎裂或MainActivity)是没有得到调用。事实上,我的应用程序startActivityForResult调用之后被破坏掉了。

But when I select place, onActivityResult(either in FragmentA or MainActivity) is not getting called. In fact,my application is getting destroyed after startActivityForResult call.

按我的理解,机器人应重新调用活动如果不能提供memory.But它不happening.Even的onCreate是没有得到所谓的内部MainActivity。

As per my understanding, android should recreate the calling activity if it is not available in memory.But it is not happening.Even onCreate is not getting called inside MainActivity.

谁能告诉我后面这种行为的原因还是我失去了什么?

Could anyone tell me the reason behind this kind of behavior or am I missing anything?

现在,而不是PlacePicker活动,我一直使用的另一项活动中同一个应用程序尝试。

Now instead of PlacePicker Activity, I have tried using another activity in same application.

让我们说我有 MainActivity 碎裂 loaded.I正在呼叫子活动 startActivityForResult 碎裂。现在,而从子活动,应用程序退出。我已经启用不要保存在我的设备来测试这种特定情况下的活动。我可以看到 MainActivity 被破坏掉了,当我移动到子活动。但是从子活动<返回/ code>,Android是不重建 MainActivity (甚至的onCreate 没有得到called.The应用程序只是退出)。

Let's say I have MainActivity with FragmentA loaded.I am calling SubActivity with startActivityForResult from FragmentA.Now while returning from SubActivity ,the application exits. I have enabled Dont keep activities in my device to test this specific scenario. I can see MainActivity getting destroyed when I move to SubActivity.But on returning from SubActivity, android is not recreating MainActivity(even onCreate is not getting called.The application just exits).

推荐答案

这似乎相当不寻常的Andr​​oid清理在您所描述的方式活动,但如果是这样的话,那么你的活动应该还是可以恢复。安卓不应破坏活动,除非你特别要求完成()什么导致pmaturely结束$ P $活动。

It seems quite unusual for Android to clean up an activity in the way you described, but if that was the case then your activity should still be restored. Android should not destroy the activity, unless you specifically call finish() or something causes the activity to end prematurely.

如果你指的是活动的生命周期图:

If you refer to the activity lifecycle diagram:

在方案中,您描述的第一个活动应该调用的onStop,但不是的onDestroy,那么当你从第二个活动回到它应该叫ONSTART了。

In the scenario you described the first activity should call onStop, but not onDestroy, then when you return from the second activity it should call onStart again.

我创建了一个非常简单的应用程序来测试你描述的情况,其中载有以下内容:

I created a very simple app to test the scenario you described, which contained the following:

  • 有2个活动,FirstActivity和SecondActivity
  • FirstActivity有一个按钮,点击该按钮时启动SecondActivity与 startActivityForResult()
  • 在活动生命周期事件在自定义的应用程序类使用 ActivityLifecycleCallbacks 登录
  • 在FirstActivity onActivityResult 另外输出到日志时,它被称为
  • There are 2 activities, FirstActivity and SecondActivity
  • FirstActivity has a button, when the button is clicked it starts SecondActivity with startActivityForResult()
  • Activity lifecycle events are logged using ActivityLifecycleCallbacks in a custom application class
  • In FirstActivity onActivityResult additionally outputs to the log when it gets called

下面是输出:

应用程序启动(FirstActivity创建并启动和可见):

Application is started (FirstActivity is created and started and visible):

FirstActivity onCreate
FirstActivity onStart
FirstActivity onResume

我preSS按钮开始SecondActivity:

I press the button to start SecondActivity:

FirstActivity onPause
SecondActivity onCreate
SecondActivity onStart
SecondActivity onResume
FirstActivity onSaveInstanceState
FirstActivity onStop

请注意,的onDestroy不会被调用。

Note, onDestroy does not get called.

现在我preSS后退按钮,返回到第一个活动:

Now I press the back button and return to the first activity:

SecondActivity onPause
FirstActivity onStart
FirstActivity onActivityResult
FirstActivity onResume
SecondActivity onStop
SecondActivity onDestroy

后退按钮调用完成上SecondActivity因此它破坏了

The back button calls finish on SecondActivity so it's destroyed

现在,如果我preSS回来FirstActivity也将完成,导致的onDestroy 被调用。

Now if I press back again FirstActivity will also be finished, causing onDestroy to be called.

FirstActivity onPause
FirstActivity onStop
FirstActivity onDestroy

您可以看到,这个例子已经坚持到生命周期图完全吻合。该活动不仅破坏一次后退按钮是pressed,这将导致活动来调用完成()

You can see that this example has adhered to the lifecycle diagram exactly. The activities are only destroyed once the back button is pressed, which causes the activity to call finish().

您提到您尝试打开的开发商选择不保留活动中,我们可以重复使用此选项启用了上述实验,看看会发生什么。我刚才说了有关生命周期事件,以节省重复的一切,这是上面的:

You mentioned that you tried turning on "Don't keep activities" in the developer options, we can repeat the above experiment which this option enabled and see what happens. I have just added the relevant lifecycle events to save repeating everything that is above:

在pressing在第一个活动的按钮,启动第二个活动:

After pressing the button in the first activity to start the second activity:

...
SecondActivity onResume
FirstActivity onSaveInstanceState
FirstActivity onStop
FirstActivity onDestroy

正如预期的那样,该活动已经被摧毁这个时候。 这是当你浏览回第一个活动再次发生了什么:

As expected, the activity has been destroyed this time. This is what happens when you navigate back to the first activity again:

SecondActivity onPause
FirstActivity onCreate
FirstActivity onStart
FirstActivity onActivityResult
FirstActivity onResume
...

这一次的onCreate又被称为系统没有第一个活动的停止版本重新启动。此外 onActivityResult()还在叫,不管事实,该活动不得不重新创建。

This time onCreate was called again as the system didn't have a stopped version of the first activity to restart. Also onActivityResult() was still called, regardless of the fact that the activity had to be recreated.

这进一步支持了东西在你的第一个活动必须在调用完成()或导致其崩溃。但是,没有看到实际的code这是猜想。

This further supports that something in your first activity must be calling finish() or causing it to crash. However, without seeing your actual code this is conjecture.

最后,为了保持状态,如果你的活动不因某些原因需要得到重建,则可以覆盖的onSaveInstanceState()并添加任何状态信息的包:

Finally, to maintain state if your activity does for some reason need to get recreated, you can override onSaveInstanceState() and add any state information to the bundle:

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(MY_STRING_KEY, "my string value");
}

当活动被重建,你会得到一个捆回来的onCreate其中应包含您要保留这些东西:

When the activity is recreated, you'll get a bundle back in onCreate which should contain everything you saved:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    if (savedInstanceState != null) {
        // Restore previous state
    }
}

这篇关于Android的parentActivity不startActivityForResult返回后得到重建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆