当我旋转屏幕时,我在保存然后将sherlockframent中的TextView文本作为选项卡附加到操作栏中时出现问题。我重写onPause()和onActivityCreated(Bundle savedInstanceState)metod,如下面的代码所示:
@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响应,问题解决了;)
答案 0 :(得分:0)
为此必须使用重写的“onCreate”或“onCreateView”方法。
答案 1 :(得分:0)
这似乎是SherlockFragment的问题,或者至少很难找到解决方案。
幸运的是,您只想保存并创建简单数据,因此我的建议是保存到sharedPreference:
在共享偏好设置中保存字符串,然后在应用中的任意位置再次检索该字符串。
public class PreferencesData {
public static void saveString(Context context, String key, String value) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
sharedPrefs.edit().putString(key, intValue).commit();
}
public static int getString(Context context, String key) {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(context);
return sharedPrefs.getString(key, defaultValue);
}
}
用法:
PreferencesData.saveString(context, "mynote", "Sherlock is weird");
// retrieve
String note = PreferencesData.getString(context, "mynote");
使用此选项可在暂停时保存字符串,并在onActivityCreated
中重新创建它希望这有帮助