I have problem with saving and then restoring text from TextView in sherlockframent attached as a tab into a actionbar when i rotate a screen. I override onPause() and onActivityCreated(Bundle savedInstanceState) metod as in belowed code:
@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");
}
Logs are saying that text is saved and then restored, but in app the textview is cleared after rotating a screen. I'm still new in programing at android so maybe i missed something. Can someone help me?
I tired use setRetainInstance with false and true but it didnt helped. Also my onCreateView looks like that:
@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;
}
I don't know if it is important but my MainActivity is:
@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"));
}
}
Problen solved thanks to cYrixmorten response;)