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

  • Home
  • SEARCH
  • 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 8567771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:57:26+00:00 2026-06-11T17:57:26+00:00

I have one Activity with multiple Fragments. I am also using actionbarSherlock for my

  • 0

I have one Activity with multiple Fragments. I am also using actionbarSherlock for my tabs which also connected to fragments.

My Problem is when I am going to rotate the screen (that is portrait to landscape/vice-versa), my activity will be called again so it restarts my activity.

I want not to restart my activity but just restore the current fragment that was shown before it was rotate. PLEASE don’t answer android:configChanges="orientation|keyboardHidden" since it does not solved the issue but just like a simple hack of it. My solution was the OnsaveInstanceState and onRestoreInstanceState Methods but I just don’t know how to use it with my problem. Please help me with this one. Any response are highly much appreciated.

CODE:

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getSupportActionBar();  
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tabA = actionBar.newTab().setIcon(R.drawable.about);
    ActionBar.Tab tabE = actionBar.newTab().setIcon(R.drawable.faq);
    ActionBar.Tab tabB = actionBar.newTab().setIcon(R.drawable.sponsors);
    ActionBar.Tab tabC = actionBar.newTab().setIcon(R.drawable.map);
    ActionBar.Tab tabD = actionBar.newTab().setIcon(R.drawable.destination);
    Fragment aboutTab = new PhotosActivity();
    Fragment sponsorTab = new SongsActivity();
    Fragment mapTab = new HCCMapActivity(); 
    Fragment questTab = new FaqActivity(); 
    Fragment DestinationTab = new TourActivity();
    tabA.setTabListener(new MyTabsListener(aboutTab));
    tabB.setTabListener(new MyTabsListener(sponsorTab));
    tabC.setTabListener(new MyTabsListener(mapTab));
    tabD.setTabListener(new MyTabsListener(DestinationTab));
    tabE.setTabListener(new MyTabsListener(questTab));
    actionBar.addTab(tabD, 0, true);
    actionBar.addTab(tabC, 1, false);
    actionBar.addTab(tabA, 2, false);
    actionBar.addTab(tabB, 3, false);
    actionBar.addTab(tabE, 4, false);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putString("MyString", "Welcome back to Android");
  //savedInstanceState.putString("id",)
  // etc.
  //getSupportFragmentManager().putFragment(savedInstanceState, "fragment", getSupportFragmentManager().findFragmentById(R.id.fragment_place));
}


@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    FragmentManager fragmentManager ;
    FragmentTransaction ft ;
    super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  String myString = savedInstanceState.getString("MyString");
  Log.i("Hello", myString);

    fragmentManager =  getSupportFragmentManager();
    ft = fragmentManager.beginTransaction();
    ft.setCustomAnimations(R.anim.slide_out_left, R.anim.slide_out_right);  
    ft.replace(R.id.fragment_place, getSupportFragmentManager().getFragment(savedInstanceState, "fragment"), null); 
}

@Override
public void onConfigurationChanged (Configuration newConfig){
    Log.i("hello", "Config");
    super.onConfigurationChanged(newConfig); 
}

@Override
public boolean onPrepareOptionsMenu (Menu menu) {
    //MenuItem menuitem1 = menu.findItem(R.id.menuitem1);
    //menuitem1.setVisible(false);

    menu.getItem(1).setVisible(false);
    menu.getItem(0).setVisible(false);
    return true;
}


 protected class MyTabsListener implements TabListener{

    Fragment fragment;

    public MyTabsListener(Fragment fragment){

        this.fragment = fragment;
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {           
        if (myTabPosition < 0){
            //ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);  
        }else{
            if (myTabPosition >  tab.getPosition()){
                ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);   
            }else{
                ft.setCustomAnimations(R.anim.slide_out_left, R.anim.slide_out_right);  
            }
        }   
        myTabPosition = tab.getPosition();
        ft.replace(R.id.fragment_place, fragment, null);    
        //ft.commit();
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.remove(fragment);
        getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
}
  • 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-11T17:57:28+00:00Added an answer on June 11, 2026 at 5:57 pm

    Fragments will be restored after a device rotation by default if you don’t add them again. If you want the fragments to look the same then you should perform your onSaveInstanceState in the Fragment itself. In the Activity you could just do something like:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState == null) {
            /* First launch, add fragments */
        } else {
            /* 
               Activity restored, don't add new fragments or in your case,
               don't make the first tab selected. 
            */
        }
    }
    

    Even if you don’t override onSaveInstanceState in the activity, the savedInstanceState parameter will still be non-null when restoring an Activity. It’ll just be an empty Bundle.

    Another option would be to store out what the selected tab index is and re-select that tab when your activity is restored.

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("CurrentTab", currentTabIndex);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* Your existing code */
    
        if(savedInstanceState != null) {
            int currentTab = savedInstanceState.getInt("CurrentTab", 0);
            /* Set currently selected tab */
        }
    }
    

    This would re-select the current tab and show the Fragment that was being shown. The only downside to this is that your fragment’s state isn’t saved. To save the fragment’s state, you’d have to do something like the first solution.

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

Sidebar

Related Questions

i have main activity in which i have Four menus. and i have one
I have an app, one activity is a form with multiple fields. In landscape
I have strange issue. My application have multiple activitys, on one activity is setting
I'm trying to implement multiple spinners within one activity, which appears to be working
I have a website that calculates a users involvement/activity using multiple MySQL queries. For
I have an activity which consists of a GridView with multiple (different) images. When
I am confused, I have multiple parts of my Activity which I have to
I have a design of activities like this I have one main activity and
I am trying to display ListView . I have created one activity , in
Within one Activity I have th following piece of code: public void onStartMonitoringToggleClicked(View v)

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.