当我旋转屏幕时,我在将作为选项卡附加到操作栏中的 Sherlockframent 中的 TextView 保存然后恢复文本时遇到问题。我覆盖 onPause() 和 onActivityCreated(Bundle savedInstanceState) 方法,如下面的代码:
@Override
public void onPause() {
super.onPause();
Bundle outState = new Bundle();
String str = memoryText.getText().toString();
outState.putCharSequence("app.multicalc.basiccalcfragment.save", str);
Log.v("saving", "text saved");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState!=null){
CharSequence str = savedInstanceState.getCharSequence("app.multicalc.basiccalcfragment.save");
memoryText.setText(str);
savedInstanceState.keySet();
Log.v("restoring", "text restored");}else Log.v("restoring","text not restored");
}
日志说文本已保存然后恢复,但在应用程序中,旋转屏幕后会清除文本视图。我在 android 编程方面还是新手,所以也许我错过了一些东西。有人能帮我吗?
我厌倦了将 setRetainInstance 与 false 和 true 一起使用,但它没有帮助。我的 onCreateView 也是这样的:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.basic_calculator, container, false);
setRetainInstance(true);
editText = (EditText) view.findViewById(R.id.txtInput);
memoryText = (TextView) view.findViewById(R.id.txtMemory);
//setting listeners for buttins from layout here
disableSoftInputFromAppearing(editText);
Log.v("basicCreate", "created basic");
return view;
}
我不知道这是否重要,但我的 MainActivity 是:
@Override
protected void onCreate(Bundle savedInstanceState) {
//setTheme(R.style.Sherlock___Theme_DarkActionBar);
setContentView(R.layout.activity_main);
actionBar = getSupportActionBar();
super.onCreate(savedInstanceState);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
tab = actionBar.newTab().setTabListener(new MyTabListener<BasicCalcFragment>(this, "Basic",BasicCalcFragment.class));
tab.setText("Basic");
actionBar.addTab(tab);
tab = actionBar.newTab().setTabListener(new MyTabListener<ScientificCalcFragment>(this, "Scientific",ScientificCalcFragment.class));
tab.setText("Scientific");
actionBar.addTab(tab);
tab = actionBar.newTab().setTabListener(new MyTabListener<ConverterFragment>(this, "Converter",ConverterFragment.class));
tab.setText("Converter");
actionBar.addTab(tab);
if(savedInstanceState!=null){
actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tabState"));
}
}
由于 cYrixmorten 的响应,问题得以解决;)