A fragment with only one instance can be created.

I'm just wondering if fragment creation can have only one instance or singleton?
I also went through the Google iosched project. They just create

Fragment a = new Fragment(); 

Whenever they want ...

Suppose, for example:

 public static FragmentManager instance; public static FragmentManager getInstance() { if (instance == null) { instance = new FragmentManager(); } return instance; } public TestFragment getTestFragment() { if (testFragment == null) { testFragment = new TestFragment (); } return testFragment } } 

Is it possible to use FragmentManager.getInstance().getTestFragment() everywhere for a transaction?

eg:

 getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, FragmentManager.getInstance().getTestFragment()) .commit(); 

Or does the OS automatically destroy the link or some related problems?

+4
source share
3 answers

When you use getSupportFragmentManager().beginTransaction().replace , you can add the third parameter as a string that you can use as a tag, so if you want to restore the previous fragment, you can use getSupportFragmentManager().findFragmentByTag(String) so you don’t have to create a new fragment.

So it will be like

Check if the fragment exists with findFragmentByTag(String) , if it does not exist, create a new fragment and call getSupportFragmentManager().beginTransaction() .replace(R.id.content_frame,myFragment,myTag).commit(); where myTag is the string you will use in your findFragmentByTag. Thus, you will not create more than one fragment of each type.

Hope this makes sense :)

For more information check this and this.

+3
source

There is no such restriction. Although, two fragment objects should not have the same tag or identifier.

In addition, it is useful to reattach an existing fragment, rather than creating a new one.

 MyFragment f = (MyFragment) getFragmentManager().findFragmenByTag("my_fragment"); if(f == null){ f = Fragment.instantiate(context, MyFragment.class.getName()); } if(!f.isAdded()){ //--do a fragment transaction to add fragment to activity, WITH UNIQUE TAG-- //--Optionally, add this transaction to back-stack as well-- } 
+2
source

If you are trying to make sure that you do not add or replace one or more of your fragments with the same β€œtype” twice or more, you can use FragmentManager.BackStackEntry to find out which of your fragments is currently on. top of the stack.

 String TAG_FIRST_FRAGMENT = "com.example.name.FIRST.tag"; String TAG_SECOND_FRAGMENT = "com.example.name.SECOND.tag"; FragmentManager fragmentManager = getSupportFragmentManager(); if (fragmentManager.getBackStackEntryCount() == 0 || !fragmentManager.getBackStackEntryAt( fragmentManager.getBackStackEntryCount() - 1) .getName().equals(TAG_SECOND_FRAGMENT)) { //Now it safe to add the secondFragment instance FragmentTransaction transaction = fragmentManager.beginTransaction(); //Hide the first fragment if you're sure this is the one on top of the stack transaction.hide(getSupportFragmentManager().findFragmentByTag(TAG_FIRST_FRAGMENT)); SecondFragment secondFragment = new SecondFragment(); transaction.add(R.id.content_frame, secondFragment, TAG_SECOND_FRAGMENT); //Add it to back stack so that you can press back once to return to the FirstFragment, and //to make sure not to add it more than once. transaction.addToBackStack(TAG_SECOND_FRAGMENT); transaction.commit(); } 
0
source

Source: https://habr.com/ru/post/1486671/


All Articles