Android Fragments, Saving State and Screen Rotation

Evelina Vrabie

Dedicated coder, occasional blogger, hackathon goer and friendly team mate.

You may also read...

25 Responses

  1. Blaine says:

    I saw this article on other website. It had identical
    meaning but in a completely different words, they use advanced article rewriter, you should read about it,
    just type in gogle:
    Niachight’s rewriter

  2. Wow, this ρarasgraph is nice, my sister is analyƶing these kinds of
    things, thus I am going to convey her.

  3. Great Post. Really helped me a lot…:)
    I did had a question though:

    1) I see that after we load the items, and if the orientation changes, the items are not reloaded and it’s great. My question is that how will I make sure that the item in a specific position in the list view stays in its position. Because when the orientation changes, the items don’t load again but the item position changes and the list is displaying from scratch. Is there any way to view the item in the position it is in.

    Thank.. will really appreciate your help.

  4. Alphonse says:

    I see a lot of interesting posts on your website. You have to spend a lot of
    time writing, i know how to save you a lot of time, there is a tool that creates unique, google friendly
    posts in couple of minutes, just type in google – laranita’s free content source

  5. Rajesh says:

    Awesome blog… keep posting….. It solve my problem within 5 minutes…
    Thanks a lot

  6. Hello, yes this paragraph is truly nice and I have learned lot of
    things from it concerning blogging. thanks.

  7. Craig says:

    really really helpful. Thanks.

  8. gingo says:

    One more note. In my opinion the mBtnReload property is a memory leak. When your activity gets restarted, it is still referenced through this reference to the Button. Or am I missing something?

  9. gingo says:

    Great article. I find only one problem, try to start background process and press hardware Home button on you phone (move activity to the background). Wait 2s (or 5s for sure) and now switch back to your activity. Oh no, the ProgressDialog is still there! Any ideas, how to solve this?

  10. João Rossa says:

    Getting the error:

    “java.lang.ClassNotFoundException: in loader dalvik.system.PathClassLoader[/data/app/]“

  11. Thank you all, I had met this kind of problem. After I read this post I found other one way by this sentence “That is because we need to re-connect – akainitLoader()” It sound might be crazy but it work on my work.
    I just call this.getSupportedLoaderManger(); in onCreate(final Bundle savedInstanceState).

  12. Andrey Zaicev says:

    Thank you for post. I had a trouble with Loaders. This post helped me.

  13. Alexjlockwood says:

    Why do you think calling “setRetainInstance(true)” is so important? The whole point of Loaders is to manage Loaders across the Activity/Fragment lifecycles. Dianne Hackborn (lead framework developer for Android) even goes as far as to suggest that you shouldn’t use “setRetainInstanceState(true)” with Loaders.

    • Evelina says:

      I’m not using setRetainInstance(true) for managing the Loaders state across screen flips. As you say, loaders already remember their state without me doing anything. But for all the rest of situations (e.g. remembering some saved variable contents in that fragment) you need setRetainInstance *UNLESS* your fragment is added to the backstack, in which case you don’t need to do anything else.

      • Alexjlockwood says:

        I just reread my comment from a couple days ago… the first sentence makes me sound like a jerk. Sorry about that haha.

        I am not sure whether or not I agree with you yet… I think I’m going to play around with the source code a bit and I’ll let you know what I think. My gut feeling is telling me that a more complete implementation would have overriden the “onStartLoading”, “onStopLoading”, etc. methods that are inherited from the Loader class. I think not implementing these methods is what is preventing the Loader from retaining its state… and if this is the case, “setRetainInstance(true)” is more of a quick fix than anything… does that make any sense? I’ll report back…

  14. dave mich says:

    Nice one, solved my problem thanks.

  15. Anonymous says:

    Hey, I just found your article, and it helped me out quite a bit. However, there is one issue that I ran into while doing this, that had me tearing my hair out for a while. For some reason, on my setup, my ListFragment subclass had “setShowList” to false. This caused a spinner to appear in the background, in addition to the ProgressDialog I was showing. And since I never set ShowList to true, the list would never be shown. In order to fix this, I added a “setShowList(true)” after I set the ListView’s adapter.

  16. Paul Turchenko says:



  17. outhorand says:

    Thank you very much for this insight. Been having trouble for two days because on rotate screen onLoadFinished() would repeat the same items again and again. Splendid and thanks alot!

  18. peng wang says:

    cool,thank you it save me time

  19. Spamularity says:

    Very helpful.

    public void onLoadFinished(Loader arg0, String arg1) {
    LoaderManager lm = getLoaderManager();

    I had to add this to the magic section in my case or it would rerun it on every rotate.

    • Alex Lockwood says:

      This is definitely not a solution to the problem… in fact, this kind of goes against the whole design goals/purpose of the LoaderManager. To correctly fix the problem, you should override the Loader’s “onStartLoading”, “onStopLoading”, etc. methods. Google search “ source code” for a sample Loader implementation.

  20. Catalin says:

    Nice article. Thanks. It cleared up some stuff for me.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>