如何维护片段中的视图状态

时间:2014-04-01 04:42:58

标签: android-fragments fragment android-fragmentactivity

如果在其上按下另一个片段时,如何保存片段中的视图状态。

Say,在android中,一个片段(FragA)被添加到backstack,另一个片段(FragB)出现在顶部。现在回击时,FragA会到达顶部并调用onCreateView()。现在,在FragB被推到它之前,我已经将FragA置于特定状态。

我的问题是如何将FragA恢复到以前的状态?有没有办法保存状态,如果有,那么我应该覆盖哪种方法?

这是我的FragmentActivity: -

package com.android.demoresponsevision.tab;

import java.util.HashMap;
import java.util.Stack;

import com.android.demoresponsevision.R;
import com.android.demoresponsevision.fragment.HomeScreen;
import com.android.demoresponsevision.fragment.SecondHomeScreen;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TextView;

public class TabActivity extends FragmentActivity implements
        OnTabChangeListener {
    TextView txt;
    private TabHost mTabHost;
    private HashMap mapTabInfo = new HashMap();
    private TabInfo mLastTab = null;
    public static Stack<Fragment> mStackTabHomeScreen = new Stack<Fragment>();
    public static Stack<Fragment> mStackTabSecondScreen = new Stack<Fragment>();



    private HomeScreen mHomeScreen;
    private SecondHomeScreen mSecondHomeScreen;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

    private class TabInfo {
        private String tag;
        private Class clss;
        private Bundle args;
        private Fragment fragment;

        TabInfo(String tag, Class clazz, Bundle args) {
            this.tag = tag;
            this.clss = clazz;
            this.args = args;
        }
    }

    class TabFactory implements TabContentFactory {
        private final Context mContext;

        /** * @param context */
        public TabFactory(Context context) {
            mContext = context;
        }

        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);
        mHomeScreen = new HomeScreen();
        mSecondHomeScreen = new SecondHomeScreen();
        fragmentManager = getSupportFragmentManager();

        initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
        }
    }

    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab
                                                                // selected
        super.onSaveInstanceState(outState);
    }

    /**
     * Step 2: Setup TabHost
     */
    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        TabActivity.addTab(this, this.mTabHost, this.mTabHost
                .newTabSpec("Tab1").setIndicator("HomeScreen"),
                (tabInfo = new TabInfo("Tab1", HomeScreen.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);

        TabActivity.addTab(this, this.mTabHost, this.mTabHost
                .newTabSpec("Tab2").setIndicator("SecondScreen"),
                (tabInfo = new TabInfo("Tab2", SecondHomeScreen.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);

        // Default to first tab
        this.onTabChanged("Tab1");
        //
        mTabHost.setOnTabChangedListener((OnTabChangeListener) this);
    }

    private static void addTab(TabActivity activity, TabHost tabHost,
            TabHost.TabSpec tabSpec, TabInfo tabInfo) { // Attach a Tab view
                                                        // factory to the spec
        tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();
        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state. If so, deactivate it, because our
        // initial state is that a tab isn't shown.

        tabInfo.fragment = activity.getSupportFragmentManager()
                .findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager()
                    .beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            activity.getSupportFragmentManager().executePendingTransactions();
        }
        tabHost.addTab(tabSpec);
    }

    /**
     * (non-Javadoc) * @see
     * android.widget.TabHost.OnTabChangeListener#onTabChanged(java.lang.String)
     */
    public void onTabChanged(String tag) {
        TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag);    

        if(mTabHost.getCurrentTabTag()=="Tab1") {

            if(mStackTabHomeScreen.size()>0) {
                 Fragment fragment=mStackTabHomeScreen.get(mStackTabHomeScreen.size()-1);
                 fragmentTransaction = fragmentManager.beginTransaction();          
                 fragmentTransaction.replace(R.id.realtabcontent, fragment);
                 fragmentTransaction.commit();
            }
            else {
             fragmentTransaction = fragmentManager.beginTransaction();          
             fragmentTransaction.replace(R.id.realtabcontent, mHomeScreen);
             mStackTabHomeScreen.add(mHomeScreen);
             fragmentTransaction.commit();

            }   

        }           
            if(mTabHost.getCurrentTabTag()=="Tab2") {

                if(mStackTabSecondScreen.size()>0) {
                     Fragment fragment=mStackTabSecondScreen.get(mStackTabSecondScreen.size()-1);
                     fragmentTransaction = fragmentManager.beginTransaction();          
                     fragmentTransaction.replace(R.id.realtabcontent, fragment);
                     fragmentTransaction.commit();
                }
                else {
                 fragmentTransaction = fragmentManager.beginTransaction();          
                 fragmentTransaction.replace(R.id.realtabcontent, mSecondHomeScreen);
                 mStackTabSecondScreen.add(mSecondHomeScreen);
                 fragmentTransaction.commit();

                }
             }

        /*if (mLastTab != newTab) 
        {
            FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) 
            {
                if (mLastTab.fragment != null) 
                {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) 
                {
                    newTab.fragment = Fragment.instantiate(this,newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontainer, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);

                }
            }
            mLastTab = newTab;
            ft.commit();
            this.getSupportFragmentManager().executePendingTransactions();

        }*/
    }

    @Override
    public void onBackPressed() {

        if(mTabHost.getCurrentTabTag()=="Tab1"){

            if(mStackTabHomeScreen.size()>1) {  

                Fragment fragment=mStackTabHomeScreen.get(mStackTabHomeScreen.size()-2);                
                fragmentTransaction = fragmentManager.beginTransaction();           
                fragmentTransaction.replace(R.id.realtabcontent, fragment);
                mStackTabHomeScreen.pop();
                fragmentTransaction.commit();
            }

            /*if(mStackTabA.size()==2){

                Fragment frag=mStackTabA.get(0);                
                fragmentTransaction = fragmentManager.beginTransaction();           
                fragmentTransaction.replace(R.id.realtabcontainer, frag);   
                mStackTabA.pop();
                fragmentTransaction.commit();
            }
            else {              
                mStackTabA.clear();
                mStackTabA.add(mFirstFragment);
            }   */      
        }

        if(mTabHost.getCurrentTabTag()=="Tab2"){

            if(mStackTabSecondScreen.size()>1) {    

                Fragment fragment=mStackTabSecondScreen.get(mStackTabSecondScreen.size()-2);                
                fragmentTransaction = fragmentManager.beginTransaction();           
                fragmentTransaction.replace(R.id.realtabcontent, fragment);
                mStackTabSecondScreen.pop();
                fragmentTransaction.commit();
            }

            /*if(mStackTabB.size()==2){

                Fragment frag=mStackTabB.get(0);                
                fragmentTransaction = fragmentManager.beginTransaction();           
                fragmentTransaction.replace(R.id.realtabcontainer, frag);   
                mStackTabB.pop();
                fragmentTransaction.commit();
            }
            else {              
                mStackTabB.clear();
                mStackTabB.add(mFirstFragment);
            }   */      
        }


    }
}

0 个答案:

没有答案