我无法重新加载我的片段状态。
架构是这样的:
我的Activity A
Fragment F
。使用选项菜单Activity A
打开SettingsActivity及其SettingsFragment(带startActivity()
)。在此期间,Fragment F
执行常规操作,调用onSaveInstanceState
,onPause
,onStop
。一切都很好。
用户点击后退按钮后,SettingsActivity会关闭,Fragment F
应重新初始化。问题开始了。 Fragment F
不会再次致电onCreateView
。它也不会调用onActivityCreated
。调用的第一个方法是onStart
。
那么在哪里可以捕获我的savedInstanceState Bundle以重新初始化我的自定义成员变量?
为什么Fragment F
成员变量(指向片段中的某些视图)仍然(或再次?)初始化。
感谢您的帮助!
以下是一些代码段:
片段F
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Log.d(this, "onCreateView");
return Butterknife.bind(this, inflater.inflate(R.layout.fragment_video_mockup, container, false));
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
Log.d(this, "onActivityCreated");
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null)
{
Log.d(this, "... loading from saved instance");
this.initFromArgument(savedInstanceState.getBundle(ARG_SAVED_STATE));
}
else if(this.getArguments() != null)
{
Log.d(this, "... loading from external arguments");
this.initFromArgument(this.getArguments());
}
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
Log.d(this, "... saving");
outState.putBundle(ARG_SAVED_STATE, createArgument(this.waveform, this.sequencer, this.paths));
}
活动A
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
Log.d(this, "options menu selected (" + item.getTitle() + ")");
switch(item.getItemId())
{
case R.id.action_settings:
this.startActivity(new Intent(MainActivity.this, SettingsActivity.class));
break;
case R.id.action_feedback:
this.onFeedbackClick();
break;
default:
throw new AssertionError("wrong option item selected");
}
return super.onOptionsItemSelected(item);
}