Paul Woitaschek
Paul Woitaschek

Reputation: 6807

Having a shared Object between Activity and Fragment: Runtime changes?

I have an Activity called BaseActivity, hosting multiple fragments.

The BaseActivity has a public field Object owhich is accessed by the fragments by calling Object o = ((BaseActivity) getActivity()).o; This is initialized in the fragments onCreate.

This works but I have problems with runtime configuration changes. It seems that the Fragments onCreate is called before the BaseActivitys onCreate, so I cant retain the Object from the Bundle i saved in onSaveInstanceState.

Is there a way I can make sure the acitivty can retain its object from the saved Bundle before the Fragment tries to access it?

Upvotes: 0

Views: 200

Answers (1)

Sjd
Sjd

Reputation: 1261

Try this ..

 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Object o = ((BaseActivity) getActivity()).o;
}

This makes sure that the activity's onCreate has completed executing.

Upvotes: 1

Related Questions