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

The Archive Base Latest Questions

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

Current, I have 2 Fragments , which is switch-able through ActionBar ‘s tab. getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

  • 0

Current, I have 2 Fragments, which is switch-able through ActionBar‘s tab.

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab newTab = getSupportActionBar().newTab();
    newTab.setText("history");
    newTab.setTabListener(new TabListenerHistoryFragment>(this, "history",
        HistoryFragment.class));

 @Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // Check if the fragment is already initialized
    if (mFragment == null) {

        // If not, instantiate and add it to the activity
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        mFragment.setRetainInstance(true);
        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        // If it exists, simply attach it in order to show it
        ft.attach(mFragment);
    }        
}

I realize the first time of my Activity (This activity is holding 2 fragments) being launched, Fragments’ methods will be called in the following sequence.

onCreate -> onCreateView -> onStart

When I perform Tab switching, and then Tab switching back to the same Fragment, the following methods will be called again.

onCreateView -> onStart

I just wish to retain the same GUI view state, when Tab is being switched back.

  • I want my chart continue to be zoomed into previous level.
  • I want my chart horizontal scroll stay at previous level.
  • I want my list continue scroll stay at previous level.
  • …

I know that I can save/restore simple variables using the following method when Tab switching

android fragment- How to save states of views in a fragment when another fragment is pushed on top of it

But, that is not something I want, as my GUI state is pretty difficult to describe within whole bunch of primitive values.

I try the following approach. Of course it won’t work, as I am getting the following runtime error.

public class HistoryFragment extends Fragment {
    View view = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (this.view != null) {
            return this.view;
        }
        this.view = inflater.inflate(R.layout.history_activity, container, false); 
    }
}

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child’s parent first.

I realize the following demo example is able to preserve its fragment GUI state (For instance, the position of vertical scroll of list) when there is Tab switching. But I guess, perhaps it is because they are using ListFragment? As I do not find they perform any special handling to preserve GUI state.

  • com.example.android.apis.app.FragmentTabs
  • com.example.android.apis.app.LoaderCursor.CursorLoaderListFragment

May I know, how I can avoid from recreating same view when perform tab switching?

  • 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-09T13:06:10+00:00Added an answer on June 9, 2026 at 1:06 pm

    I had the same problem, and tried to follow the suggestion in the error message.
    I tried the following code, and it worked for me.

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state)  {
         if (mMyView == null) {
             mMyView = new MyView(getActivity());
         } else {
             ((ViewGroup) mMyView.getParent()).removeView(mMyView);
         }
    
         return mPuzzleView; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have current a developed app which I am going to submit in just
At current i have an index page which shows values from an XML page
We have this current database which we need to replace some tables by anther
I have some .jspf files that are fragments which I include in a new
My current project is getting awfully large. I have dozens of activities, adapters, fragments,
I have page which consists of couple fragments and in the header fragment I
I have an app using fragments, all of which are contained in a single
I have an app where the MainActivity which creates an ActionBar with tabs (used
The code i current have is this. function update (){ latest_id = $('#image:first').data('position'); /*
Current I have a seperate maven module for my database access, all my DAO

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.