开发者

Avoid Android automatically re-add my fragment on orientation change

I have a ListFragment which depends on the hosting Activity to properly initialize. On first run, it loads up fine. Once I change the orientation, my app crashes. From the stack trace I can see it isn't me trying to add the Fragment prematurely, rather Android is trying to restore the Fragment.

I have setRetainInstance(false) set in the onStart method but can't find any method to disable the restoring of the Fragment once the orientation changes. Any ideas? Do I need to remove the Fragment prior to my app being destroyed?

开发者_Python百科

Edit: I ended up delaying initializing the list until the Activity is ready. Android conveniently shows a 'loading' message until the adapter is set.


I ended up delaying initializing the list until the Activity is ready. Android conveniently shows a 'loading' message until the adapter is set.


I have a ListFragment which depends on the hosting Activity to properly initialize.

That may be your difficulty right there.

From the stack trace I can see it isn't me trying to add the Fragment prematurely, rather Android is trying to restore the Fragment.

Correct.

Any ideas?

I would focus on handling configuration changes properly. Between onSaveInstanceState() in the fragment and the combination of onRetainNonConfigurationInstance(), getLastNonConfigurationInstance() (both on Activity) and onAttach() (on your Fragment), you should be able to pass whatever stuff is in the old fragment to the new one without crashing. For configuration changes, do not rely upon "the hosting Activity to properly initialize".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜