这是我的代码:
活动主.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_One"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1.70"
android:text="Activity_main.xml - Design Time Text"
android:textSize="12sp"
android:onClick="btnOne_OnClick"
android:textStyle="normal" />
布局2.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_Two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Layout2.xml - Design Time Text"
android:textSize="12sp"
android:textStyle="normal" />
在主代码中,请注意注释:
public class MainActivity extends Activity implements ActionBar.TabListener {
ActionBar.Tab Tab_1 = null, Tab_2 = null;
Button btn_One = null, btnTwo = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
///////////////////////////////////////////////////////////////////
ActionBar actionbar = (ActionBar) getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab_1 = actionbar.newTab().setText("Tab 1");
Tab_2 = actionbar.newTab().setText("Tab 2");
Tab_1.setTag(1);
Tab_2.setTag(2);
Tab_1.setTabListener(this);
Tab_2.setTabListener(this);
actionbar.addTab(Tab_1);
actionbar.addTab(Tab_2);
///////////////////////////////////////////////////////////////////
btn_One = (Button)this.findViewById(R.id.btn_One);
}
public void btnOne_OnClick(View v) {
((Button)v).setText("Clicked");
btn_One = ((Button)v);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {}
// Here is what the problem is:
// After you click btn_one, the text changes to "Clicked" - good! - however
// after Tab 1 gets reselected, the text off btn_One (btn_One) gets reset back to the one
// set in design time: "Activity_main.xml - Design Time Text", so the "Clicked" text disappears
// The interesting thing is, when you rotate the screen, the text gets re-initialized to "Clicked" and that's
// because of onRestoreInstanceState(). So it looks like when a tab gets reselected, the design time screen
// gets re-drawn and onRestoreInstanceState doesn't get recalled.
// My "workaround" for the above issue would be to create a global
// "Bundle" poiner and pass the address of the one from onSaveInstanceState(Bundle savedInstanceState)
// to the global Bundle pointer and then call onRestoreInstanceState(*the_global_bundle_pointer*)
// when a tab gets reselected so that it restores the state.
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int selectedTab = (Integer) tab.getTag();
if (selectedTab == 1) this.setContentView(R.layout.activity_main);
if (selectedTab == 2) this.setContentView(R.layout.layout2);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
if (btn_One != null) savedInstanceState.putString("btn_One", btn_One.getText().toString());
super.onSaveInstanceState(savedInstanceState);
}
// When the instance is restored, reload all data
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
if (btn_One != null) btn_One.setText(savedInstanceState.getString("btn_One").toString());
}
}
另外,我说两个选项卡的原因是我可以以最简单的方式演示问题,以便人们可以理解我要解决的问题。我的真实项目使用了两个以上的选项卡。
对于上述问题,我的“解决方法”是创建一个全局“Bundle”指针,并将该指针的地址从onSaveInstanceState(Bundle savedInstanceState)传递给全局 Bundle 指针,然后在重新选择选项卡时调用onRestoreInstanceState (*the_global_bundle_pointer*)使其恢复状态。
你怎么看?