Pergunta

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?

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

Foi útil?

Solução

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

Outras dicas

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".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top