Android 返回frm后台时如何保存活动和片段?

Android 返回frm后台时如何保存活动和片段?,android,android-fragments,Android,Android Fragments,我正在用abc类创建应用程序扩展了FragmentActivity,有4个选项卡。每个选项卡都有片段一些选项卡有多个片段我正在使用hashmap来处理。它工作得很好,但应用程序在后台运行了很长时间,并返回前台的“重新创建”选项卡和片段。我的问题是如何保持并返回以显示相同的视图而不重新创建 public class AppMainTabActivity extends FragmentActivity { /* Your Tab host */ private TabHost mTabHos

我正在用abc类创建应用程序扩展了FragmentActivity,有4个选项卡。每个选项卡都有片段一些选项卡有多个片段我正在使用hashmap来处理。它工作得很好,但应用程序在后台运行了很长时间,并返回前台的“重新创建”选项卡和片段。我的问题是如何保持并返回以显示相同的视图而不重新创建

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();
}
公共类AppMainTabActivity扩展了FragmentActivity
{
/*您的选项卡主机*/
私有TabHost-mTabHost;
/*堆栈的HashMap,其中我们使用选项卡标识符作为键*/
公共HashMap mstack;
/*将当前选项卡标识符保存在此*/
公共字符串mCurrentTab;
私有字符串mOldTab;
创建时受保护的void(Bundle savedInstanceState){
setContentView(R.layout.app\u main\u tab\u fragment\u布局);
super.onCreate(savedInstanceState);
mStacks=newhashmap();
mStacks.put(AppConstants.TAB_HOME,new Stack());
mStacks.put(AppConstants.TAB_TRIP,new Stack());
mStacks.put(AppConstants.TAB_消息,新堆栈());
mStacks.put(AppConstants.TAB_菜单,new Stack());
mTabHost=(TabHost)findViewById(android.R.id.TabHost);
mTabHost.setOnTabChangedListener(侦听器);
mTabHost.setup();
初始化tabs();
}
public void initializeTabs(){
/*设置选项卡图标和内容视图。这没有什么特别的*/
TabHost.TabSpec spec=mTabHost.newTabSpec(AppConstants.TAB_HOME);
mTabHost.setCurrentTab(-3);
spec.setContent(新TabHost.TabContentFactory(){
公共视图createTabContent(字符串标记){
返回findViewById(R.id.realtabcontent);
}
});
规格设置指示器(createTabView(“主”,R.drawable.tab_a_state_btn));
mTabHost.addTab(规范);
spec=mTabHost.newTabSpec(AppConstants.TAB\u TRIP);
spec.setContent(新TabHost.TabContentFactory(){
公共视图createTabContent(字符串标记){
返回findViewById(R.id.realtabcontent);
}
});
规格设置指示器(createTabView(“Trip”,R.drawable.tab_b_state_btn));
mTabHost.addTab(规范);
spec=mTabHost.newTabSpec(AppConstants.TAB_消息);
spec.setContent(新TabHost.TabContentFactory(){
公共视图createTabContent(字符串标记){
返回findViewById(R.id.realtabcontent);
}
});
spec.setIndicator(createTabView(“Message”,R.drawable.tab_c_state_btn));
mTabHost.addTab(规范);
spec=mTabHost.newTabSpec(AppConstants.TAB_菜单);
spec.setContent(新TabHost.TabContentFactory(){
公共视图createTabContent(字符串标记){
返回findViewById(R.id.realtabcontent);
}
});
规格设置指示器(createTabView(“更多”,R.drawable.tab_d_state_btn));
mTabHost.addTab(规范);
}
/*当用户切换选项卡时,或者我们以编程方式进行切换时,会出现此对话框*/
TabHost.OnTabChangeListener=新TabHost.OnTabChangeListener(){
已更改的公共无效项(字符串选项卡ID){

对于(int i=0;i尝试在onPaush()上存储当前日期,并在resume()上设置以前的数据).但我添加了片段,所以如何使用简历在暂停和检索时存储ui检查:ok@Hareshchelana我检查并回复感谢您的帮助这是不同的,我的情况不同,在后台长时间重新创建ui,回到前台,我的问题@Hareshchelana我的问题是在长时间保存ui而不重新创建从背景到前景