findFragmentByTag() returns null after perform a FragmentTransaction using replace() method

前端 未结 8 1638
广开言路
广开言路 2020-11-30 04:44

My Android app consists three fragments: A, B and C. They\'re loaded in the two containers defined in the MainActivity layout.

When th

相关标签:
8条回答
  • 2020-11-30 05:04

    In my case I used the code to replace and add to BackStack, but set wrong tag:

    val fragment = { SomeFragment.newInstance() }
    fragmentManager?.replaceAndAddToBackStack(R.id.container, fragment, WrongAnotherFragment.TAG)
    

    Of course, supportFragmentManager.findFragmentByTag(SomeFragment.TAG) didn't find SomeFragment.

    0 讨论(0)
  • 2020-11-30 05:05

    I've fixed it! I called getSupportFragmentManager().executePendingTransactions() after doing the transaction and it worked! After calling that method I can get the fragment using both findFragmentById() and findFragmentByTag() methods.

    0 讨论(0)
  • 2020-11-30 05:07

    We are also seeing this problem but the cause is slightly different. The suggested solution by https://stackoverflow.com/a/21170693/1035008 doesn't work for us.

    void updateFragment(Fragment newFragment) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
    
        // We have these 2 lines extra
        Fragment current = getChildFragmentManager().findFragmentByTag(fragmentTag);           
        if (current != null) { ft.remove(current); }
    
        ft.replace(R.id.right_container, newFragment, fragmentTag); //Replaces the Fragment C previously in the right_container with a new Fragment B
        ft.commit(); //Finishes the transaction
    
        //!!HERE THE APP CRASHES (java.lang.NullPointerException = findFragmentByTag returns null
        ((FragmentB) getFragmentManager().findFragmentByTag(fragmentTag)).testView();
    }
    

    And after reading the documentation about replace:

    Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

    I realize that the remove call was not necessary since it is done by replace automatically. So after delete ft.remove(current), it works fine.

    0 讨论(0)
  • 2020-11-30 05:08

    I had the same problem and realized that there is a really simple way to fix this. When using a tag please do make sure to add the

    fragmentTransaction.addToBackStack(null); 
    

    method so that your Fragment is resumed instead of destroyed as mentioned in the developer guides.

    If you don't call addToBackStack() when you perform a transaction that removes a fragment, then that fragment is destroyed when the transaction is committed and the user cannot navigate back to it. Whereas, if you do call addToBackStack() when removing a fragment, then the fragment is stopped and is later resumed if the user navigates back.

    You can find this at the end of this section.

    Every time I tried to reference back to my created Fragment, it turns out it had already been destroyed so I lost about 30 minutes trying to figure out why my Fragment was not being found through a simple findFragmentByTag(); call.

    Hope this helps!

    0 讨论(0)
  • 2020-11-30 05:08

    For me probably it was a newbie mistake that I was calling super.onCreate(savedInstanceState); after I was trying to access the Fragment using findFragmentByTag.

    I moved super.onCreate(savedInstanceState) up in the order and it started working for me.

    0 讨论(0)
  • 2020-11-30 05:10

    if you use setRetainInstance(true) than you can't use findFragmentByTag() in onCreate from the Activity. Do it at onResume

    see the documentation: setRetainInstance

    0 讨论(0)
提交回复
热议问题