Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uses of fragment tags

In Android, you can set a tag for a Fragment in a FragmentTransaction.

Why would I need to set a tag for a Fragment?

And is it good practice if a Fragment changed its behavior based on its tag?

like image 826
Mahmoud Hanafy Avatar asked Nov 22 '12 09:11

Mahmoud Hanafy


People also ask

What is the use of fragment tag?

Fragment tags can be used to avoid recreating a Fragment on Activity orientation change. The Activity is recreated on orientation change, and its onCreate(...) method is called.

What is the use of fragment manager?

FragmentManager is the class responsible for performing actions on your app's fragments, such as adding, removing, or replacing them, and adding them to the back stack.

How do you tag a fragment?

The only way to set a Fragment tag that I have found is by doing a FragmentTransaction and passing a tag name as parameter.

What is the use of addToBackStack in Android?

addToBackStack. Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.


2 Answers

Fragment tags can be used to avoid recreating a Fragment on Activity orientation change.

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.photos_image_pager);      MyFragment fragment;     if (savedInstanceState != null) {         fragment = (MyFragment) getFragmentManager()             .findFragmentByTag("my_fragment_tag");     } else {         fragment = new MyFragment();         fragment.setArguments(getIntent().getExtras());         getFragmentManager()             .beginTransaction()             .add(android.R.id.content, fragment, "my_fragment_tag")             .commit();      } } 

The Activity is recreated on orientation change, and its onCreate(...) method is called. If the Fragment was created before the Activity was destroyed and was added to the FragmentManager with a tag, it can now be retrieved from the FragmentManager by the same tag.

For a longer discussion on how it can be used, see:

  • ViewPager and fragments - what's the right way to store fragment's state?
like image 177
onosendai Avatar answered Sep 22 '22 02:09

onosendai


For Non-UI Fragments:

Why would I need to set a tag for a Fragment?

Sometimes the Fragment can be used as a background worker by the Activity. Such fragment doesn't have UI, it is also called non-UI fragment. The String tag is the only way to identify this fragment. To add this fragment, you use add(Fragment, String) method which doesn't take View Id. For example:

FragmentManager fm = getFragmentManager(); workFragment.setTargetFragment(this, 0); fm.beginTransaction().add(workFragment, "work").commit(); 

Then later, to get the reference to this fragment use,

workFragment = (WorkFragment)fm.findFragmentByTag("work"); 

Now using the workFragment reference you can access all the methods of this fragment in the Activity or it's UI fragment. Here's the complete example of a non-UI fragment.


For Fragments with UI:

And is it good practice if a Fragment changed its behaviour based on its tag?

There are situations where we have to change the fragment's behaviour from Activity using tags. It should be alright to do so, I've seen some open source apps using this practice. Basically you supply string tags to fragments. This is used for tagging a fragment within the FragmentManager so we can easily look it up later. And change or access it's behaviour from the Activity.

For example: A user switches to SettingsActivity from MainActivity, changes the setting, presses back button to come back to MainActivity. You can detect the setting change in MainActivity#onResume() and update or change the behaviour of the current fragment according to the new setting.

FragmentManager fm = getFragmentManager(); uiFragment = (UiFragment) fm.findFragmentByTag("ui"); uiFragment.fetchNewData(); uiFragment.displayNewData(); 
like image 33
Yogesh Umesh Vaity Avatar answered Sep 20 '22 02:09

Yogesh Umesh Vaity