Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:09:48+00:00 2026-06-15T13:09:48+00:00

the docs on setRetainInstance say : This can only be used with fragments not

  • 0

the docs on setRetainInstance say :

This can only be used with fragments not in the back stack.

so I started playing with it.

I have one Activity with adds first frag A

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, new PackageFragment());
ft.commit

then from this frag I run a method from parent Activity which adds frag B to backstack

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, new OrderFragment());
ft.addToBackStack(null);
ft.commit();

then I create log msg from onCreate,onDestroy,onSaveInstanceState,onActivityCreated…etc

I try two versions of this process. Rotating the device on each fragment.

  1. default

Everything is as expected. onCreate, onDestroy on fragments fire

  1. setRetainInstance(true)

Everything is as expected?. onCreate, onDestroy on fragments dont fire

and all seems to work while fragments are in the backstack.. so why the docs say I shouldnt use it?
What are the scenarios where I might get in trouble?

thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:09:49+00:00Added an answer on June 15, 2026 at 1:09 pm

    Updated answer:

    What are the scenarios where I might get in trouble?

    When adding a Fragment to the back stack and passing a Bundle in the Fragment from onSaveInstanceState() to onCreateView() on configuration change. Calling setRetainInstance(true) will set the Bundle to null on configuration change.

    (I’m not sure a developer would actually attempt this since using setRetainInstance(true) makes onSaveInstanceState() kind of redundant, but I didn’t see the behaviour documented in the API docs so I wrote up this answer).

    If both addToBackStack() and setRetainInstance(true) are called, setRetainInstance() partly alters the Fragment lifecycle method calls and parameter values on configuration changes, compared to calling only addToBackStack().

    Specifically, in the test below, looking a differences between calling only addToBackStack() and calling setRetainInstance(true) as well, and seeing what happens on configuration change:

    Calling addToBackStack() but not setRetainInstance(true);

    • onCreate() and onDestroy() are called.
    • a bundle passed from onSaveInstanceState() is received as a parameter in onCreateView().

    Calling both addToBackStack() and setRetainInstance(true):

    • onCreate() and onDestroy() are not called. This is metioned in the API docs.
    • a bundle passed from onSaveInstanceState() is not received in onCreateView(). The passed-in Bundle is null.

    A test with logged method calls and parameters tested for null:

    In the Activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        MyFragment fragment;
        if (savedInstanceState != null) {
            fragment = (MyFragment) getFragmentManager().findFragmentByTag("my_fragment_tag");
        } else {
            fragment = new MyFragment();
            FragmentTransaction t = getFragmentManager().beginTransaction();
            t.addToBackStack(null);//toggle this
            t.add(android.R.id.content, fragment, "my_fragment_tag").commit(); 
        }
    }
    

    In the Fragment:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);//toggle this
    }
    

    and

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("test", "value");
        super.onSaveInstanceState(outState);
    }
    

    Test 1: Fragment lifecycle when addToBackStack() is called , and setRetainInstance(true) is not called

    • onAttach()
    • onCreate()
    • onCreateView()
    • onActivityCreated()
    • onStart()
    • onResume()

    [Device rotated from portrait to landscape]

    • onPause()
    • onSaveInstanceState()
    • onStop()
    • onDestroyView()
    • onDestroy()
    • onDetach()
    • onAttach()
    • onCreate()
    • onCreateView() with bundle param != null
    • onStart()
    • onResume()

    Test 2 & 3: Fragment lifecycle calls with setRetainInstance(true) called, addToBackStack() called / not called (same result):

    • onAttach()
    • onCreateView()
    • onActivityCreated()
    • onStart()
    • onResume()

    [Device rotated from portrait to landscape]

    • onPause()
    • onSaveInstanceState()
    • onStop()
    • onDestroyView()
    • onDetach()
    • onAttach()
    • onCreateView() with bundle param == null
    • onStart()
    • onResume()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MSDN docs say that only value types need boxing, but this does not apply
MySQL docs say that tables that use the MERGE storage engine can only union
Docs,forums etc. keeps referencing the Spring petclinic example. Where can I get this for
The docs say that I should not release any modeled property in -dealloc. For
Docs say you shouldn't nest fragments 'cause they are designed to work another way.
The docs for qr/STRING/ say: This operator quotes (and possibly compiles) its STRING as
Django docs say that Context object is a stack: from django.template import Context c
Rails docs have this information for Object#blank? An object is blank if it’s false,
The docs say All permutations occur with approximately equal likelihood. but I do not
The docs say this for the dismiss() method from the Dialog class: Dismiss this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.