Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreateView() isn't called immediately after FragmentTransaction.commit()

I have an activity where I dynamically replace fragments:

private void goToFragment(Fragment newFragment, String tag) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_container, newFragment, tag);
    ft.addToBackStack(null);
    ft.commit();
}

Now, I want to access the views inside the fragment so I can put data (that I have stored in my activity) into them, immediately after calling goToFragment.

The problem is, the fragment's onCreateView isn't called before the fragment is rendered completely, at least to my understanding.

I know overriding the onAttach(Activity activity) in the fragment is one way to go about it, but then I have to cast it specifically to my activity - and I just want to avoid that because I consider it bad practice for the fragment to be dependent on a specific activity.

As far as I can see, Fragment doesn't have any listeners (as a subject) implemented.

So I figure I have to make my own listener (Using the Observer Pattern to make the fragment a subject and the activity an observer), and then calling it whenever the onCreateView or onAttach is done, and then finally calling back to the fragment with the data that needs to be set. However, I need to do this for several fragments so I would have to make a listener for each fragment, which I again think is bad.

Is there any better/easier way to do this?

like image 731
Aske B. Avatar asked Jun 02 '12 18:06

Aske B.


People also ask

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

When onActivityCreated is called?

So basically onActivityCreated is called after the host activity onCreate has returned.

What is difference between onCreateView () and onActivityCreated () methods of fragment lifecycle?

The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() . Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.

When to use onAttach in Fragment?

onAttach() is always called before any Lifecycle state changes. The onDetach() callback is invoked when the fragment has been removed from a FragmentManager and is detached from its host activity. The fragment is no longer active and can no longer be retrieved using findFragmentById() .


2 Answers

FragmentTransaction isn't applied instantly after calling commit(). You may force update manually:

...
mFragmentManager.executePendingTransactions();

AFAIK event callbacks' purpose is custom communication with Fragment beyond it's usual lifecycle.

like image 160
Andrey Ermakov Avatar answered Nov 14 '22 11:11

Andrey Ermakov


The correct way to do it would be to define an interface for Activity classes wishing to display your Fragment should implement. That way, on onAttach you don't cast to a specific Activity but to your interface.

See for instance: http://developer.android.com/guide/topics/fundamentals/fragments.html#EventCallbacks

like image 41
K-ballo Avatar answered Nov 14 '22 10:11

K-ballo