我无法重新加载我的片段状态。
架构是这样的:
我有Activity A
一个Fragment F
. 使用选项菜单Activity A
打开带有 SettingsFragment 的 SettingsActivity(带有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));
}
活动一
@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);
}