简体   繁体   中英

Add a Fragment after onPause

In my FragmentActivity I'm doing a HTTP GET and updating the UI with the results. I'm using a DialogFragment for displaying possible errors (like server not responding, etc.)

The problem is that sometimes my app is reporting a java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState when I call show() over the DialogFragment because the user pauses the Activity before the network returns.

My problem is that if I ignore the show() if the activity is not resumed the error is never shown, how can I add the Fragment (or queue the Fragment in the FragmentManager or something similar) in order to the FragmentDialog to be there if the user resume the Activity?

Should I raise some kind of flag (saving the error type and params) in the activity and show the fragment in the onResume()? or there is an easier method?.

Thanks

One way to solve it would be to separate the data from the view.

You can have events (such as network response arrived) change only your data objects.

The view will display whatever is in the data when it is asked to do so.

When the view is visible (not paused for one) and the data changed, just notify it there's a change in the data and ask it to update it's display (without necessarily detailing what the change was).

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