將我的savedInstanceState寫入了我的片段。片段中的savedInstanceState不起作用
保存實例不起作用。當我切換回「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();
}
}
這就是工程..片段改變正常..
敬酒只能如果該片段ACTIVE ..但如果我切換回給他。
例如:
- 首頁=>首頁=> 「烤麪包出現」
- 首頁=>縣=>首頁=> 「否吐司」。
進入我的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());
}
不起作用:-s對不起 –
這是否意味着'savedInstanceState'仍然是空或究竟是怎麼回事? – ishmaelMakitla
我認爲onSave從未被稱爲.. –