Get the current fragment and save the screen orientation method in onSaveInstanceState ()

I have one action with multiple fragments. I also use actionbarSherlock for my tabs, which are also linked to fragments.

My problem is when I rotate the screen (i.e. portrait to landscape / vice versa), my activity will be called again to restart my activity.

I do not want to restart my activity, but simply restore the current fragment that was shown before it was rotated. PLEASE do not answer android:configChanges="orientation|keyboardHidden" as it does not solve the problem, but just like a simple hack. 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. Any answer is 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 } } 
+8
android actionbarsherlock android-actionbar android-fragments screen-orientation
source share
1 answer

Fragments will be restored after turning the device by default, if you do not add them again. If you want the fragments to look the same, you must execute your onSaveInstanceState in the Fragment itself. In Activity you can 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 do not override onSaveInstanceState in the activity, the savedInstanceState parameter will still be invalid when restoring the Activity . It will be an empty Bundle .

Another option is to save what the selected tab index selected, and re-select this 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 will re-select the current tab and show the displayed Fragment . The only drawback to this is that the state of your fragment is not preserved. To save the state of the fragment, you need to do something like the first solution.

+13
source share

All Articles