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 9076593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:08:55+00:00 2026-06-16T19:08:55+00:00

I’ve got a tabbed application using Fragment s, and a slight problem. MainActivity (extends

  • 0

I’ve got a tabbed application using Fragments, and a slight problem.

MainActivity (extends FragmentActivity – with no code save for the layout specification on onCreate) includes a Fragment called TabsFragment (extends Fragment, implements OnTabChangeListener) in its layout (which houses the tabs themselves in a FrameLayout), switching out sub-Fragments using Transactions.

The latter sub-Fragments are the ones with the actual content whose state I wish to save when the user turns the device, visits another application, answers a phone call, or does something else. Currently the state of the sub-Fragments are not saved, but the activity remembers which sub-Fragment is being shown. (So if I have the device in landscape mode and turn the device while tab 3 is active, tab 3 will appear in portrait mode, reset, but showing. So some state is being preserved without me having done anything).

Because I have multiple layouts (landscape and portrait), android:configChanges="orientation" in the manifest is not an option – I wouldn’t want to use it anyway given that it’s a glorified bandaid.

I’m using the v4 support library (rev 10) and after scouring the API, I came across FragmentManager.saveFragmentInstanceState() and Fragment.setInitialSavedState(Fragment f). I have put the saveFragmentInstanceState() for each sub-Fragment in the overridden onPause() method of TabsFragment. I’m not sure if this works, because wherever I put the setInitialSavedState, it gives an IllegalStateException– but this happens even when I have just initialised a new sub-Fragment tab. It also crashes when I put it into onResume in TabsFragment.

Code snippet:

//... this is the 'showTab' method
if(getFragmentManager().findFragmentByTag(id) == null)
{
  Fragment f = null;
  if(BASIC_TAB.equals(id))
  {
    f = new BasicTabFragment();
    f.setInitialSavedState(basicState); // basicState was set in onPause()
    getFragmentManager().beginTransaction().replace(tabNo, f, tabID).commit();
  }
}

If similar code is placed in onResume, I get the following error:

Error snippet:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity (org.example.App/org.example.App/MainActivity): java.lang.IllegalStateException: Fragment already active

-snip-

Caused by: java.lang.IllegalStateException: Fragment already active at  android.support.v4.app.Fragment.setIniailSavedState(Fragment.java:507)
at org.example.App.TabsFragment.onResume(TabsFragment.java:223)

etc.

Clearly I'm calling setInitialSavedState at the wrong point in the lifecycle, but it's not clear to me when this should happen.

Also I'm beginning to wonder if FragmentManager.saveFragmentInstanceState() is indeed the best thing to use here given the somewhat complex nature of the tabbed setup. If so, what to use? How can I save the state of my application's tabs?

  • 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-16T19:08:56+00:00Added an answer on June 16, 2026 at 7:08 pm

    When an Activity is destroyed, the Fragments are detached from the Activity and re-attached when it is recreated. Therefore, the fragments should still exist in their current state. When re-attached, OnCreateView is called again. If you’re initializing everything as if it’s from scratch (ie. setting lists to new ArrayList()) you will be wiping out the state of the fragment.

    Try making an instance variable within your Fragment for everything that needs to be preserved (ie. a String for any text fields – EditText fields will be automatically saved as you mention). Example:

    String text;
    TextView tv;
    
    public View onCreateView(LayoutInflater i, ViewGroup vg, Bundle b)
    {
       // this will re-inflate everything from the layout. If you initialize any text fields here, it will reset them to the value you set in the xml file
       View v = i.inflate(R.layout.myLayout, vg, false);
    
       if(text != null)
           tv.setText(text);
    }
    

    When your TextView value changes (say in an OnClickListener or similar), set the value of text in addition to whatever else you do with it. When the fragment is re-attached, it will then set it to the last change that was made.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns 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.