I am having trouble reloading my fragments state.
The architecture is like this:
I am having an Activity A
with a Fragment F
. With the options menu Activity A
opens the SettingsActivity with its SettingsFragment (with startActivity()
). During this time the Fragment F
does his usual thing, calls onSaveInstanceState
, onPause
, onStop
. All good here.
As soon as the user hits the back button the SettingsActivity closes and the Fragment F
should be re-initialized. Here the problem starts. The Fragment F
does not call onCreateView
again. It also does not call onActivityCreated
. The first method that is called is onStart
.
So where can I catch my savedInstanceState Bundle in order to reinitialize my custom member variables?
And why are the Fragment F
member variables (that point to some Views in the fragment) still (or again?) initialized.
Thanks for any help!
Here are some code snippets:
Fragment 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));
}
Activity 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);
}