简体   繁体   中英

PreferenceActivity, Headers, and onDestroy being called when the activity is visible

I'm running into a bit of a problem.

I have a PreferenceActivity with a list of headers, each pointing to a fragment. Those fragments are shown as a single pane (small display).

In order to exit a header fragment and go back to the list of headers of the PreferenceActivity itself, I press the back button (as a user), or call getActivity().onBackPressed() if I need to get back to the list after the user pressed a button on the UI.

This brings me back to the header list PreferenceActivity, but it also calls the onDestroy() method of that activity.

This is what I don't understand:

Why does it call the onDestroy() when the activity itself is clearly visible? And why doesn't it call the onCreate() after that, again, since the activity is visible?

This also has a side effect of calling the onReset() of a loader I use to create the list of data that produces the headers in the first place. That in turn makes it look like the header list hasn't changed, even when I have removed an item from the list, and thus reduced the number of headers. If I actually close and reopen the PreferenceActivity, the header list will be correct, which shows that the loader itself is working.

I don't fully understand what is happening here without code. But from the sounds of it you are calling onBackPressed() for the activity, thus what happens happens irrespective of fragments. Try overriding onBackPressed() in the activity and handle your fragment transactions there.

Headers launch new activities in single pane mode. That is likely why you are seeing onDestroy() for the same named activity, as it launches the same activity with a different intent it seems.

For future readers.

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