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

相关问题