0

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);
}
Martin Mlostek
  • 2,755
  • 1
  • 28
  • 57
  • Is that your state is correct after back, and you just want to know why? Is my understanding correct? – Derek Fung Sep 16 '15 at 08:53
  • No, the state is not correct. My references to views are alive, but references to my custom objects are `null` so I need to reinitialize them – Martin Mlostek Sep 16 '15 at 08:55
  • Which are your references to view, and which are your references to custom objects? – Derek Fung Sep 16 '15 at 09:01
  • Well, `waveform`, `sequencer` and `paths` are my custom objects. Then I have some references to some buttons that are bound with ButterKnife. – Martin Mlostek Sep 16 '15 at 09:14
  • Actually the behaviour your described are all expected, except the part about your references to custom objects, are you sure they are set probably? Maybe you should add extra logs in `onSaveInstanceState` to see if the references are not null. – Derek Fung Sep 16 '15 at 09:23
  • Well, I added breakpoints in all methods. Oncreateview and onactivitycreated are not called before onstop after coming back from the settingsactivity – Martin Mlostek Sep 16 '15 at 09:28
  • oncreateview and onactivitycreated not being called is correct/expected. Only because this doesn't solve your "problem" (your custom references), I would leave it later to explain that in details. – Derek Fung Sep 16 '15 at 09:30
  • And why is it expected? Shall the fragment be persistent in memory even though onSavedInstanceState and onStop is called? – Martin Mlostek Sep 16 '15 at 09:30
  • yes, exactly, http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle – Derek Fung Sep 16 '15 at 09:32

0 Answers0