Fragment within Viewpager calls no method

Asked

Viewed 431 times

0

I have an activity (MainActivity) containing a PagerFragment who owns a ViewPager. On the pager I have 4 different tabs.

Within each tab I own one Fragment 1 which can be replaced by using the FragmentManager.replace() and add it to backStack (addToBackStack())

Everything works normally, but I noticed a problem that I couldn’t find anywhere:

When that Fragment 1 , within the ViewPager, calls the replace(new Fragment2() ).addToBackStack(null), my Fragment is replaced correctly.

However, when I press the back button, Fragment2 is destroyed correctly, but the Fragment1 who was in the backstack, doesn’t call his onCreateView, or onResume.

Another thing I noticed, is that when I call the replace, the onDestroy of Fragment1 is not called.

What is wrong?

EDIT

Talking to @Wakim, we came to the conclusion that I am replacing Fragments in the wrong way: In the onCreateView of Fragment1: mView = inflater.inflate(R.layout.fragment_lists, container, false);

And in the onClickListener of a button responsible for replace:

FragmentManager fragmentManager = mContext.getSupportFragmentManager();
                fragmentManager.beginTransaction()
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                        .replace(R.id.view_lists_fragment, new CreateListFragment())
                        .addToBackStack(null)
                        .commit();

The R.id.view_lists_fragment is the container of my layout Fragment1

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:id="@+id/view_lists_fragment"
tools:context="com.kayan.letsapp.Fragments.ListsFragment"> 
.... 
</RelativeLayout>
  • 1

    What do you call "father fragment"? The Fragment that hosts the ViewPager or the Fragment previous within the ViewPager?

  • Edited question, @Wakim

  • 1

    Kayan, is using the FragmentPagerAdapter to make that management or is adding the Fragments on its own account?

  • I’m using the FragmentStatePagerAdapter to return the 4 different fragments in tabs. replace... I’m taking a look at this project now: https://github.com/danilao/fragments-viewpager-example/blob/master/src/com/pineappslab/frcontainer/RootFragment.java And it looks like it creates a Rootfragment, I guess I’m not replacing the fragments properly inside the pager view

  • 1

    Hmmm, I think the problem is in that replace. The ideal is that it does not interfere with the management FragmentStatePagerAdapter makes, it may be that he is getting lost... What you want is to exchange a certain Fragment within one position for another and then be able to return?

  • That, except when I come back, the Fragment I was before should call onResume, or onCreate, so I can update certain things. I need at least something to be called by Fragment1!

  • 1

    Can you see if the onDestroyView and the onCreateView are being called? For me these two should be called because of the replace and of backStack.

  • onDestroyView is not being called, I’m definitely doing this replace wrong! I think I’m using the id wrong!

  • @Wakim, I edited my question, if I could help an Eng. friend of ufrj computing, I would be eternally grateful ;D

  • 1

    I think that’s right, but only that you would need to add one Fragment within the onCreateView of Fragment1 to be removed. I believe you’re doing it right (I’ll call it Rootfragment)? If so, in the onCreateView of Fragment1 you would add the RootFragment in the view_lists_fragment and then would replace him by CreateListFragment. In that case when you get back, it’s onCreateView and onResume of RootFragment that will be called. The Fragment1 has always been visible/active so its life cycle is not changed.

  • @Wakim, I understood, The correct thing then is to have a dummy Fragment that contains Fragments, because this Fragment Dummy, being in Viewpager, is not destroyed, right?

  • 1

    That’s right. I took a look at the answer to see if I made it clear hehe

Show 7 more comments

1 answer

1


Following the source code of the project, what I understood has to be done is the following:

1 - Creation of the main Fragment

FragmentStatePagerAdapter calls the getItem, soon you must return the Fragment main position, this guy I call RootFragment (I believe in your case it would be Fragment1, but I will use another nomenclature, following the project you mentioned).

The RootFragment adds the Fragment1 which is initially visible. The Fragment1 is the initial son.

I think it would be even better to use the getChildFragmentManager, but I don’t know if it’s necessary.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.root_fragment, container, false);

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.root_frame, new Fragment1());
    transaction.commit();

    return view;
}

2 - Replace of Fragment1 for CreateListFragment.

I would delegate to RootFragment that responsibility:

public void replaceFragment() {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.replace(R.id.root_frame, new CreateListFragment());
    transaction.addToBackStack(null);

    transaction.commit();
}

In the end you’d have something like:

Fragmentstatepageradapter -> add Rootfragment

Rootfragment -> add Fragment1

Rootfragment -> replace Fragment1 to Createlistfragment

3 - Removal of Createlistfragment

When you do FragmentManager.popBackStack() the CreateListFragment will be destroyed (onDestroView and onDestroy will be called).

And the Fragment1 will be restored (onResume and onCreateView will be called).

Remembering that I’m not so sure how this works, because I never had to implement something like this. But I think it should work

  • I will implement this here and I already say, anyway it was of an absurd help! Thanks even!

  • Strange... even though it’s without the onChildFragmentManager(), when I press the back, it destroys the activity that possesses the ViewPager, even putting in backStack

  • 1

    At what point? After adding the CreateListFragment?

  • Yes, the strange thing is this: Changing the Fragmentmanager when it comes to booting my Adapter, the code works! AdminPagerAdapter(getFragmentManager()); Can you explain why? It’s working, but for me Viewpager should have a Childfragmentmanager, since they are nested Fragments, right? Now it’s all messed up!

  • about your question, it correctly adds the CreateListFragment, but when I squeeze back, it destroys the Fragment containing the ViewPager whole.

  • 1

    No, the AdminPagerAdapter needs to have the getFragmentManager and not the getChildFragmentManager. The concept of nested Fragments applies only to RootFragment and not to the ViewPager. I think it’s best to use the FragmentManager in both cases, because from what I understand the BackStack is not "shared" and when you press the Back Button, he checks only the BackStack of FragmentManager of the highest level and not of FragmentManager's of children.

  • Last question: In what situation would the ViewPagerAdapter use the ChildFragmentAdapter?

  • I think maybe, if you had the following situation: Activity -> Fragment -> ViewPager (Fragment's). I’ve never actually worn this FragmentManager, but I think that would be a case.

  • Worse than I think it’s exactly my situation, but it’s working perfectly now! Thank you very much, @Wakin!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.