0

可能是重複問題。但我無法找到我的問題的解決方案如何在TabChange中保存/恢復片段中的視圖狀態

我有一個TabHost與3個選項卡。比方說tabA,tabB,tabC。當我切換到另一個選項卡(即tabA或tabB)並且返回到tabC時,我做了很多視圖更改,如按鈕文本更改,tabC中的editText更改,我失去了所有視圖更改。我所有的視圖值(按鈕和editTexts),它將我所有的視圖重置爲默認的XML文件值。我不希望發生這種情況。 我只想如何保存我的意見的狀態。我不能在製表符變更時失去這一點。

我試過這個link但失敗了。

這裏是我的代碼

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    Toast.makeText(getActivity(), "onCreateView", Toast.LENGTH_SHORT) 
      .show(); 

    if (container == null) { 

     return null; 
    } 

    View vi = inflater.inflate(R.layout.settings, container, false); 

    btnInsert = (Button) vi.findViewById(R.id.btnInsert); 
    btnInsert.setOnClickListener(this); 
    btnPosition = (Button) vi.findViewById(R.id.btnPosition); 
    btnPosition.setOnClickListener(this); 
    txtPosition = (TextView) vi.findViewById(R.id.txtPosition); 
    txtLogo = (TextView) vi.findViewById(R.id.txtLogo); 
    imgLogoPreview = (ImageView) vi.findViewById(R.id.imgLogoPreview); 
    imgLogoPreview.setOnClickListener(this); 
    edTxtUserText = (EditText) vi.findViewById(R.id.edTxtPreview); 
    relLogo = (RelativeLayout) vi.findViewById(R.id.RelLogo); 
    relText = (RelativeLayout) vi.findViewById(R.id.RelText); 

    logoWheel = (WheelView) vi.findViewById(R.id.wheelLogo); 

    logoWheel.setAdapter(new ArrayWheelAdapter<String>(logoWheelList)); 
    logoWheel.setVisibleItems(4); 
    logoWheel.setCurrentItem(1); 
    positionWheel = (WheelView) vi.findViewById(R.id.wheelPosition); 
    positionWheel.setAdapter(new ArrayWheelAdapter<String>(
      positionWheelTextList)); 

    // LogoWheel changed listener 
    changedListenerLogo = new OnWheelChangedListener() { 
     public void onChanged(WheelView wheel, int oldValue, int newValue) { 
      if (!wheelScrolled) { 

      } 
     } 
    }; 

    logoWheel.addChangingListener(changedListenerLogo); 

    // Wheel scrolled listener 
    scrolledListenerLogo = new OnWheelScrollListener() { 

     public void onScrollStarts(WheelView wheel) { 
      wheelScrolled = true; 

     } 

     public void onScrollEnds(WheelView wheel) { 
      wheelScrolled = false; 
      btnInsert.setText(logoWheelList[wheel.getCurrentItem()] + ""); 
      wheel.setVisibility(View.INVISIBLE); 
      if (wheel.getCurrentItem() == 2) { 

       txtPosition.setVisibility(View.INVISIBLE); 
       btnPosition.setVisibility(View.INVISIBLE); 
       relText.setVisibility(View.INVISIBLE); 
       relLogo.setVisibility(View.INVISIBLE); 

      } else if (wheel.getCurrentItem() == 1) { 

       relText.setVisibility(View.VISIBLE); 
       relLogo.setVisibility(View.INVISIBLE); 
       txtPosition.setVisibility(View.VISIBLE); 
       btnPosition.setVisibility(View.VISIBLE); 
       btnPosition.setText("Top"); 
       positionWheel.setAdapter(new ArrayWheelAdapter<String>(
         positionWheelTextList)); 
       positionWheel.setVisibleItems(4); 
       positionWheel.setCurrentItem(1); 

      } else if (wheel.getCurrentItem() == 0) { 

       relLogo.setVisibility(View.VISIBLE); 
       relText.setVisibility(View.INVISIBLE); 
       txtPosition.setVisibility(View.VISIBLE); 
       btnPosition.setVisibility(View.VISIBLE); 
       btnPosition.setText("Top Left"); 
       positionWheel.setAdapter(new ArrayWheelAdapter<String>(
         positionWheelLogoList)); 
       positionWheel.setVisibleItems(4); 
       positionWheel.setCurrentItem(1); 

      } 
     } 
    }; 

    logoWheel.addScrollingListener(scrolledListenerLogo); 

    // /////////////////////Positon Wheel Listners/////////// 

    // LogoWheel changed listener 
    changedListenerPosition = new OnWheelChangedListener() { 
     public void onChanged(WheelView wheel, int oldValue, int newValue) { 
      if (!wheelScrolled) { 

      } 
     } 
    }; 

    positionWheel.addChangingListener(changedListenerPosition); 

    // Wheel scrolled listener 
    scrolledListenerPosition = new OnWheelScrollListener() { 

     public void onScrollStarts(WheelView wheel) { 
      wheelScrolled = true; 

     } 

     public void onScrollEnds(WheelView wheel) { 
      wheelScrolled = false; 

      String btnStatus = btnInsert.getText().toString(); 
      if (btnStatus.equals("Logo")) { 
       btnPosition.setText(positionWheelLogoList[positionWheel 
         .getCurrentItem()] + ""); 

      } else if (btnStatus.equals("Text")) { 
       btnPosition.setText(positionWheelTextList[positionWheel 
         .getCurrentItem()] + ""); 
      } 

      wheel.setVisibility(View.INVISIBLE); 

     } 
    }; 

    positionWheel.addScrollingListener(scrolledListenerPosition); 

    return vi; 

} 

編輯 這裏是我米以下this教程我FrgmentActivity

public class MainActivity extends FragmentActivity implements 
    TabHost.OnTabChangeListener { 

private TabHost mTabHost; 
private HashMap mapTabInfo = new HashMap(); 
private TabInfo mLastTab = null; 

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; 
    } 

    /** 
    * (non-Javadoc) 
    * 
    * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String) 
    */ 
    public View createTabContent(String tag) { 
     View v = new View(mContext); 
     v.setMinimumWidth(0); 
     v.setMinimumHeight(0); 
     return v; 
    } 

} 

/** 
* (non-Javadoc) 
* 
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) 
*/ 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Step 1: Inflate layout 
    setContentView(R.layout.tabs_layout); 
    // Step 2: Setup TabHost 
    initialiseTabHost(savedInstanceState); 
    if (savedInstanceState != null) { 
     mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); // set 
    } 
} 

/** 
* (non-Javadoc) 
* 
* @see android.support.v4.app.FragmentActivity#onSaveInstanceState(android.os.Bundle) 
*/ 
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; 
    MainActivity.addTab(
      this, 
      this.mTabHost, 
      this.mTabHost.newTabSpec("Tab1").setIndicator("Home", 
        getResources().getDrawable(R.drawable.instructions)), 
      (tabInfo = new TabInfo("Tab1", Instructions.class, args))); 
    this.mapTabInfo.put(tabInfo.tag, tabInfo); 
    MainActivity.addTab(
      this, 
      this.mTabHost, 
      this.mTabHost.newTabSpec("Tab2").setIndicator("Capture", 
        getResources().getDrawable(R.drawable.capture)), 
      (tabInfo = new TabInfo("Tab2", Capture.class, args))); 
    this.mapTabInfo.put(tabInfo.tag, tabInfo); 
    MainActivity.addTab(
      this, 
      this.mTabHost, 
      this.mTabHost.newTabSpec("Tab3").setIndicator("Settings", 
        getResources().getDrawable(R.drawable.settings)), 
      (tabInfo = new TabInfo("Tab3", Settings.class, args))); 
    this.mapTabInfo.put(tabInfo.tag, tabInfo); 
    // Default to first tab 
    this.onTabChanged("Tab1"); 
    // 
    mTabHost.setOnTabChangedListener(this); 
} 

/** 
* @param activity 
* @param tabHost 
* @param tabSpec 
* @param clss 
* @param args 
*/ 
private static void addTab(MainActivity 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 (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.realtabcontent, newTab.fragment, newTab.tag); 
      } else { 
       ft.attach(newTab.fragment); 
      } 
     } 

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

} 

的代碼。

+0

什麼是您的代碼爲選項卡?應該保存狀態,如果你不重新創建你的分片當標籤更改 – 2013-02-22 14:18:29

+0

@YallaT我編輯了我的問題並粘貼了標籤的代碼。請看一看。謝謝 – 2013-02-23 04:29:00

回答

0

對於片段的少量的,你可以嘗試這個

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

在的onCreate設置爲true()

+0

這不是我想要的。即時通訊工作在onCreatView() – 2013-02-23 04:59:12

+0

setRetainInstance將保持視圖狀態在內存中,所以沒關係,如果你從標籤A切換到C,如果你回到A它將是相同的,這樣你沒有要實現savedInstance,並且仍將調用onCreateView(),請檢查Fragment生命週期和setRetainInstance()行爲(上面的鏈接) – outlying 2013-02-23 09:59:05

+0

docs says「控制片段實例是否在Activity重新創建期間保留(例如,從配置更改) 。這隻能用於不在後端堆棧中的片段。如果設置,則在重新創建活動時,片段生命週期將略有不同:「 – 2013-02-23 10:09:14

1

這裏是你的問題的解決,我是這是對你有幫助.. 。即時發佈的一段代碼:

TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() { 

      @Override 
      public void onTabChanged(String tabId) { 
       android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); 
       AndroidFragment androidFragment = (AndroidFragment) fm.findFragmentByTag("android"); 
       AppleFragment appleFragment = (AppleFragment) fm.findFragmentByTag("apple"); 
       android.support.v4.app.FragmentTransaction ft = fm.beginTransaction(); 

       /** Detaches the androidfragment if exists */ 


       /** Detaches the applefragment if exists */ 


       /** If current tab is android */ 
       if(tabId.equalsIgnoreCase("android")){ 

        if(androidFragment==null){  
         /** Create AndroidFragment and adding to fragmenttransaction */ 
         ft.add(R.id.realtabcontent,new AndroidFragment(), "android");      
        }else{ 
         /** Bring to the front, if already exists in the fragmenttransaction */ 
         if(appleFragment!=null) 
         ft.hide(appleFragment); 

         ft.show(androidFragment);      
        } 

       }else{ /** If current tab is apple */ 
        if(appleFragment==null){ 
         /** Create AppleFragment and adding to fragmenttransaction */ 
         ft.add(R.id.realtabcontent,new AppleFragment(), "apple");      
        }else{ 
         /** Bring to the front, if already exists in the fragmenttransaction */ 
         if(androidFragment!=null) 
          ft.hide(androidFragment); 

         ft.show(appleFragment);      
        } 
       } 
       ft.commit();     
      } 
     }; 


     /** Setting tabchangelistener for the tab */ 
     tHost.setOnTabChangedListener(tabChangeListener); 

只實現這一點,它工作正常,我救的意見狀態....

+0

這個伎倆。但必須稍微改變一下代碼。無論如何,我們必須隱藏'androidFragment',即使'appleFragment'是之前創建的。顯示'androidFragment'時同樣適用於'appleFragment'。所以隱藏部分應該在'null'上面檢查'if'。 – 2015-09-08 12:41:19