Can fragment be created with only one instance

家住魔仙堡 提交于 2019-12-05 01:55:26

问题


I was just wondering, can fragment creation only have one instance or singleton?
I went through Google iosched project too. They simply create

Fragment a = new Fragment();

Whenever they want...

Suppose eg:

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 
    }
}

Can I use everywhere FragmentManager.getInstance().getTestFragment() for transaction?

eg:

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

Or OS automatically destroy the reference or some issues related to it?


回答1:


When you use getSupportFragmentManager().beginTransaction().replace you can add a third parameter as a string that you can use as a tag, so in case you want to recover a previous fragment you can use getSupportFragmentManager().findFragmentByTag(String) so you won't have to create a new fragment.

So it would be like this

Check if the fragment exists using findFragmentByTag(String) if it not exists, create a new fragment and call getSupportFragmentManager().beginTransaction() .replace(R.id.content_frame,myFragment,myTag).commit(); where myTag is the String you'll use in your findFragmentByTag. This way you won't create more than one fragment of every type.

I hope it makes some sense :)

For more information check this and this




回答2:


No such limitation. Though, two fragment objects must not have same tag or id.

Also, its good to re-attach an existing fragment, rather that 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--
}



回答3:


If you are trying to make sure that you will not add or replace one or more of your fragments with the same "type" twice or more, then you can use the FragmentManager.BackStackEntry to know which of your fragments is currently on the 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's 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();

} 


来源:https://stackoverflow.com/questions/17154341/can-fragment-be-created-with-only-one-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!