![](/img/trans.png)
[英]Android Studio: SavedInstanceState of object doesn't work
[英]savedInstanceState in Fragment doesn't work
我的savedInstanceState進入我的Fragment時出了點問題。
保存實例不起作用。 當我切換回“ HomeMenu”時,所有字段均消失。
在我的活動中:
private void selectItemFromDrawer(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = null;
String tagFrag = "default";
switch (mNavItems.get(position).mTitle) {
case R.string.menu_home:
fragment = fragmentManager.findFragmentByTag("menu_home");
if(fragment == null) {
fragment = new HomeMenuFragment();
} else {
Toast.makeText(customActivity.this, "already Exists", Toast.LENGTH_SHORT).show();
}
tagFrag = "menu_home";
break;
case R.string.menu_pref:
fragment = fragmentManager.findFragmentByTag("menu_pref");
if(fragment == null)
fragment = new PreferencesFragment();
tagFrag = "menu_pref";
break;
}
if(fragment != null) {
fragmentManager.beginTransaction()
.replace(R.id.mainContent, fragment, tagFrag)
.commit();
}
}
多數民眾贊成在工作..片段更改正確..
僅當“片段”處於活動狀態時,TOAST才起作用。但是,如果我切換回給他,則不起作用。
例:
進入我的HomeMenuFragment我使用以下代碼:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_main, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
strUser = savedInstanceState.getString("strUser", "");
strName = savedInstanceState.getString("strName", "");
strDesc = savedInstanceState.getString("strDesc", "");
if(((TextView) getActivity().findViewById(R.id.user))!=null)
((TextView) getActivity().findViewById(R.id.user)).setText(strUser);
if(((TextView) getActivity().findViewById(R.id.name))!=null)
((TextView) getActivity().findViewById(R.id.name)).setText(strName);
if(((TextView) getActivity().findViewById(R.id.desc))!=null)
((TextView) getActivity().findViewById(R.id.desc)).setText(strDesc);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("strUser", ((TextView) getActivity().findViewById(R.id.user)).getText().toString());
outState.putString("strName", ((TextView) getActivity().findViewById(R.id.name)).getText().toString());
outState.putString("strDesc", ((TextView) getActivity().findViewById(R.id.desc)).getText().toString());
}
好吧,我會嘗試解釋一下,這並不容易。
在這種情況下,您的片段不會被破壞,只會破壞其中的視圖。 因此,您使用的每個視圖都必須實現狀態保存/恢復。
我給你留下了一個更好的解釋,我的希望對你有幫助! https://inthecheesefactory.com/blog/fragment-state-saving-best-practices/en
更新資料
嘗試使用newIstance創建片段,這是避免丟失參數的最佳實踐。 例如:
public static MyFragment newInstance(String user, String name, String desc) {
MyFragment mFragment = new MyFragment();
Bundle args = new Bundle();
args.putString("user", user);
args.putString("name", name);
args.putString("desc", desc);
mFragment.setArguments(args);
return mFragment;
}
在片段onCreate()
您可以使用以下方式訪問數據:
String strUser = getArguments().getString("user", "");
如果片段是由Android重新創建的,則捆綁包將可用。 請記住,只能在將Fragment附加到Activity之前調用setArguments。
讓我知道!
根據Bundle文檔,既沒有putString()
也沒有getString()
方法,您需要使用putStringArrayList (String key, ArrayList<String> value)
。 我還建議您避免對getActivity()。findViewById()進行太多調用,眾所周知這會導致大量開銷。 另外,為什么要從片段中的活動獲取視圖值? 看起來像重復的代碼。 無論如何,請嘗試以下操作:
TextView tvUser, tvName, tvDesc;
@Override
public void onCreate(Bundle savedInstanceState) {
// Store TextView references calling findViewById only once
tvUser = ((TextView) getActivity().findViewById(R.id.user));
tvName = ((TextView) getActivity().findViewById(R.id.user));
tvDesc = ((TextView) getActivity().findViewById(R.id.user));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
ArrayList al = new ArrayList();
al = savedInstanceState.getStringArrayList("args");
tvUser.setText(al.get(0));
tvName.setText(al.get(1));
tvDesc.setText(al.get(2));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
ArrayList al = new ArrayList();
al.add(0, tvUser.getText());
al.add(1, tvName.getText());
al.add(2, tvDesc.getText());
outState.putStringArrayList("args", al);
}
可能是在放置希望保存的字符串之前調用了super.onSaveInstanceState(outState)
。 為了解決這個問題,您只需要在放置希望保存的值之后調用super.onSaveInstanceState(outState)
。 這是您需要進行的更改:
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("strUser", ((TextView) getActivity().findViewById(R.id.user)).getText().toString());
outState.putString("strName", ((TextView) getActivity().findViewById(R.id.name)).getText().toString());
outState.putString("strDesc", ((TextView) getActivity().findViewById(R.id.desc)).getText().toString());
//moved it here
super.onSaveInstanceState(outState);
}
我希望這能解決您的問題。 試試看,讓我知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.