Findfragmentbytag() Returns Null After Perform a Fragmenttransaction Using Replace() Method

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

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.

GetFragmentManager.findFragmentByTag() returns null

I was having the same problem of findFragmentByTag() always returning null.

Eventually I tracked it down, I was overriding onSaveInstanceState() in my Activity but not calling super. As soon as I fixed that findFragmentByTag() returned the Fragment as expected.

findFragmentByTag returning null in android

When you using commit() method nobody gives to you guarantee that your fragment will be attached to FragmentManager on the fly.

This happens due to inner FragmentManager logic : when you add\replace\remove any fragments you create a FragmentTransaction and puts it into execution queue inside FragmentManager. Actually all Transactions waiting until FragmentManager enqueue tasks.

To avoid this situation you can force enqueue each task under Fragment via

getSupportFragmentManager().executePendingTranscation();

This method starts pending Transactions and make more hard sure for use findFragmentById() method.

So, finally you need :

CustomFragment fragment = new CustomFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//add or replace or remove fragment
ft.commit();

getSupportFragmentManager().executePendingTranscation();

CustomFragment customFragment = (CustomFragment) getSupportFragmentManager().findFragmentByTag("FragmentTag");

Why does fragmentManager.findFragmentByTag return allways null?

You must add the line addToBackStack(null) to your FragmentTransaction before committing. This makes it so when a previous Fragment is removed from view, it will just be stopped and not destroyed.

This is stated in the docs here:

Note: When you remove or replace a fragment and add the transaction to the back stack, the fragment that is removed is stopped
(not destroyed). If the user navigates back to restore the fragment,
it restarts. If you do not add the transaction to the back stack, then
the fragment is destroyed when removed or replaced.

findFragmentByTag() always return null - Android

i found my mistake, it is i forgot to add TAG to back stack.

FragmentManager fm = mainActivity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.activity_main_content_fragment, fragment, text);
ft.addToBackStack(text);

And then i can get the current fragment TAG name as follows,

FragmentManager fm = MainActivity.this.getSupportFragmentManager();
String currentFragmentTag = fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName();

findFragmentByTag() returns null on existing fragmentTag

The name you get back from fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 2).getName() is the name, you passed to addToBackStack(String name). That's an entirely separate concept from the tag associated with a fragment.

If you want to set a tag associated with a fragment, you need to set one as part of your replace() call using the version that takes a tag:

getSupportFragmentManager().beginTransaction()
.replace(R.id.create_tasks_fragment_container, fragment, fragmentTag)
.addToBackStack(fragmentTag)
.commit();

Note: you should never mix a fragments transactions that use addToBackStack() with transactions that do not use addToBackStack() as this will break the behavior of popBackStack() and the system back button (which assumes you have not changed any of the fragments since the last addToBackStack() transaction).

Android fragments - findFragmentByTag always returns null

In your code you haven't mentioned tag in replace method So,

Use this structure of replace method of fragment

ft.replace(R.id.container, newFragment,"fragment_tag_String");

Refer this link for more information. fragment replace with tag name



Related Topics



Leave a reply



Submit