Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListFragment and onActivityCreated called twice

i have a listFragment this is my onActivityCreated:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        utenti=savedInstanceState.getParcelableArrayList("array");
    }else{
        threadutenti= (GetUtenti) new GetUtenti(this.getActivity(), utenti).execute();
    }

When i rotate my device i have first savedInstanceState non null (i got it) but after onActivityCreated is called with onActivityCreated null!! why? i want get my object utenti on ListFragment and not on my activity..

like image 526
fabio Avatar asked Mar 06 '13 10:03

fabio


People also ask

Is Onviewcreated called multiple times?

Both onCreate and onCreateView get called every time I get to a tab.

Why is Onviewcreated twice in Android app using navigation components?

OnCreateView is called twice without pressing back button or on any other event. Its called twice, once when fragment initially creates and again just after findNavController. navigate event.


1 Answers

This can happen if you're always creating a new fragment in your Activity#onCreate() callback.
If it's true, check this answer. Your Fragment#onActivityCreated will be called twice:

  • First time triggered by FragmentActivity#onStart's call to mFragments.dispatchActivityCreated(). It will pass the saved instance.
  • Second time triggered by FragmentActivity#onStart's call to mFragments.execPendingActions(). This time without saved instance (since it is being called in response to new Fragment addition).
like image 110
Alex Lipov Avatar answered Sep 17 '22 23:09

Alex Lipov