0

i am creating app with class abc extends FragmentActivity and have 4 tabs. each tab have fragment some tab have more than one fragment i am using hashmap to handle. it works well but application running on background for long time and return back foreground recreate tab and fragment.my question is how to persist and return back to show same view without recreating

public class AppMainTabActivity extends FragmentActivity
   {
/* Your Tab host */
private TabHost mTabHost;

/* A HashMap of stacks, where we use tab identifier as keys..*/
public HashMap<String, Stack<Fragment>> mStacks;

/*Save current tabs identifier in this..*/
public String mCurrentTab;
private String mOldTab;

protected void onCreate(Bundle savedInstanceState) {

    setContentView(R.layout.app_main_tab_fragment_layout);
    super.onCreate(savedInstanceState);

    mStacks = new HashMap<String, Stack<Fragment>>();
        mStacks.put(AppConstants.TAB_HOME, new Stack<Fragment>());
        mStacks.put(AppConstants.TAB_TRIP, new Stack<Fragment>());
        mStacks.put(AppConstants.TAB_MESSAGE, new Stack<Fragment>());
        mStacks.put(AppConstants.TAB_MENU, new Stack<Fragment>());
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setOnTabChangedListener(listener);
        mTabHost.setup();

        initializeTabs();
}
public void initializeTabs(){
    /* Setup your tab icons and content views.. Nothing special in this..*/
    TabHost.TabSpec spec    =   mTabHost.newTabSpec(AppConstants.TAB_HOME);
    mTabHost.setCurrentTab(-3);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView("Home", R.drawable.tab_a_state_btn));
    mTabHost.addTab(spec);


    spec                    =   mTabHost.newTabSpec(AppConstants.TAB_TRIP);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView("Trip", R.drawable.tab_b_state_btn));
    mTabHost.addTab(spec);

    spec                    =   mTabHost.newTabSpec(AppConstants.TAB_MESSAGE);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView("Message", R.drawable.tab_c_state_btn));
    mTabHost.addTab(spec);

    spec                    =   mTabHost.newTabSpec(AppConstants.TAB_MENU);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(R.id.realtabcontent);
        }
    });
    spec.setIndicator(createTabView("More", R.drawable.tab_d_state_btn));
    mTabHost.addTab(spec);
}


/*Comes here when user switch tab, or we do programmatically*/
TabHost.OnTabChangeListener listener    =   new TabHost.OnTabChangeListener() {
  public void onTabChanged(String tabId) {

      for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
      {

          TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(R.id.tab_text);
        if (i == mTabHost.getCurrentTab())
          tv.setTextColor(Color.parseColor("#48036d"));
        else
            tv.setTextColor(Color.parseColor("#9DA0A3"));

      }


    /*Set current tab..*/
      mOldTab = mCurrentTab;
    mCurrentTab                     =   tabId;
      if(!(mOldTab != null && !mOldTab.isEmpty())) {


          mOldTab = mCurrentTab;

      }


    if((mStacks.get(tabId).size() == 0) && (mStacks.get(mOldTab).size() == 0) ){
      /*
       *    First time this tab is selected. So add first fragment of that tab.
       *    Dont need animation, so that argument is false.
       *    We are adding a new fragment which is not present in stack. So add to stack is true.
       */
      if(tabId.equals(AppConstants.TAB_A)){
        pushFragments(tabId, new HomeFragment(), false,true);
      }else if(tabId.equals(AppConstants.TAB_B)){
          pushFragments(tabId, new TripFragment(), false,true);
      }else if(tabId.equals(AppConstants.TAB_C)){
          pushFragments(tabId, new MessageFragment(), false,true);
      }else if(tabId.equals(AppConstants.TAB_D){
        pushFragments(tabId, new MenuFragment(), false,true);
      }
    }else {

        popAllFragments(mOldTab);
        if(tabId.equals(AppConstants.TAB_A)){
            pushFragments(tabId, new HomeFragment(), false,true);
        }else if(tabId.equals(AppConstants.TAB_B)){
            pushFragments(tabId, new TripFragment(), false,true);
        }else if(tabId.equals(AppConstants.TAB_C)){
            pushFragments(tabId, new MessageFragment(), false,true);
        }else if(tabId.equals(AppConstants.TAB_D)){
            pushFragments(tabId, new MenuFragment(), false,true);
        }
      /*
       *    We are switching tabs, and target tab is already has atleast one fragment. 
       *    No need of animation, no need of stack pushing. Just show the target fragment
       */
    }
  }
};
public void pushFragments(String tag, Fragment fragment,boolean shouldAnimate, boolean shouldAdd){
  if(shouldAdd)
      mStacks.get(tag).push(fragment);
  FragmentManager   manager         =   getSupportFragmentManager();
  FragmentTransaction ft            =   manager.beginTransaction();
  if(shouldAnimate)
      ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
  ft.replace(R.id.realtabcontent, fragment);
  ft.commit();
}
sabeer
  • 603
  • 10
  • 28
  • Try to store current date on onPaush() and set previous data onResume(). – Haresh Chhelana Oct 06 '15 at 06:06
  • but i added fragment so how to store ui in pause and retrive using resume – sabeer Oct 06 '15 at 06:12
  • Check out this : http://stackoverflow.com/questions/11353075/how-can-i-maintain-fragment-state-when-added-to-the-back-stack – Haresh Chhelana Oct 06 '15 at 06:16
  • ok @HareshChhelana i check and reply thanks for ur help – sabeer Oct 06 '15 at 06:21
  • it's different my case is different Activty recrete ui when long time on background and back to foreground that my issue @HareshChhelana my question is any way to save ui without recreate when longtime in background to foreground – sabeer Oct 06 '15 at 06:32

0 Answers0