当活动被销毁并重新打开时,恢复微调器状态,在textViews等中输入的文本的最佳方法是什么?
编辑: 这里是我的一些代码,当再次调用onCreate时,如何在writeText中输入任何文本?
SharedPreferences sh_Pref;
SharedPreferences.Editor toEdit;
@Override
public void onDestroy() {
sharedPrefernces();
super.onDestroy();
}
public void sharedPrefernces() {
sh_Pref = getSharedPreferences("SavedConfig", MODE_PRIVATE);
toEdit = sh_Pref.edit();
toEdit.putString("Text", writeText.getText().toString());
toEdit.commit();
}
答案 0 :(得分:1)
如果为textView设置唯一标识符,则活动将自动保存该元素的状态。出于其他目的,您应该覆盖onSavedInstanceState()
方法
答案 1 :(得分:1)
我尝试了很多东西,但是这个是我发现并使用过几次的最好的方法。用例子清楚地说明了,在this帖子
中精美地解释了public class MainFragment extends StatedFragment {
...
/**
* Save Fragment's State here
*/
@Override
protected void onSaveState(Bundle outState) {
super.onSaveState(outState);
// For example:
//outState.putString("text", tvSample.getText().toString());
}
/**
* Restore Fragment's State here
*/
@Override
protected void onRestoreState(Bundle savedInstanceState) {
super.onRestoreState(savedInstanceState);
// For example:
//tvSample.setText(savedInstanceState.getString("text"));
}
...
}
还提供片段模板以便更好地理解
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.inthecheesefactory.thecheeselibrary.R;
/**
* Created by nuuneoi on 11/16/2014.
*/
public class StatedFragment extends Fragment {
Bundle savedState;
public StatedFragment() {
super();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Restore State Here
if (!restoreStateFromArguments()) {
// First Time, Initialize something here
onFirstTimeLaunched();
}
}
protected void onFirstTimeLaunched() {
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save State Here
saveStateToArguments();
}
@Override
public void onDestroyView() {
super.onDestroyView();
// Save State Here
saveStateToArguments();
}
////////////////////
// Don't Touch !!
////////////////////
private void saveStateToArguments() {
if (getView() != null)
savedState = saveState();
if (savedState != null) {
Bundle b = getArguments();
b.putBundle("internalSavedViewState8954201239547", savedState);
}
}
////////////////////
// Don't Touch !!
////////////////////
private boolean restoreStateFromArguments() {
Bundle b = getArguments();
savedState = b.getBundle("internalSavedViewState8954201239547");
if (savedState != null) {
restoreState();
return true;
}
return false;
}
/////////////////////////////////
// Restore Instance State Here
/////////////////////////////////
private void restoreState() {
if (savedState != null) {
// For Example
//tv1.setText(savedState.getString("text"));
onRestoreState(savedState);
}
}
protected void onRestoreState(Bundle savedInstanceState) {
}
//////////////////////////////
// Save Instance State Here
//////////////////////////////
private Bundle saveState() {
Bundle state = new Bundle();
// For Example
//state.putString("text", tv1.getText().toString());
onSaveState(state);
return state;
}
protected void onSaveState(Bundle outState) {
}
}