简体   繁体   中英

Prevent an Activity from being killed by the OS while starting a child activity

I have a main activity which calls a child one via

Intent I = new Intent(this, Child.class);
startActivityForResult(I, 0);

But as soon as Child becomes visible the main activity gets its onStop and immediately after that onDestroy method triggered. And as soon as I call finish() within the Child activity or press the back button, the Child activity closes and the home screen shows (instead of the main activity).

How can I prevent the main activity from being destroyed? :\\

If you launch a child Activity from which you expect return data, you'll probably want to use startActivityforResult instead.

You may want to check this question: Child Activity in Android as it seems to be the same problem.

Edit:

As for what's happening here, you could place code in the onStop() and/or onDestroy() methods to investigate - at least a call to isFinishing() to check why the Activity is being destroyed.
You should also use adb logcat from your host machine to check the logcat in case it holds more information - and maybe use Log.d() (the result goes into logcat as well) instead of toasts to make sure you don't miss them.

check androidmanifest nohistory=true and that made the OS destroy the activity before the result. that might be one of the reason for your problem.

我使用了Dialog而不是Activity,并且一切正常,所以我就这样保留它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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