0

I have an Activity with ListView, when clicking on a list item opens a new fragment. Do I need to create a new fragment every time like this?

     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
     fragmentTransaction.add(R.id.root_layout,new MyFragment());

Or will be enough to create a fragment once and then use it?

in activity:

     MyFragment myFragment = new MyFragment();
     ......

in onItemClickListener:

     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
     fragmentTransaction.add(R.id.root_layout,myFragment);
Dmitriy Puchkov
  • 1,530
  • 17
  • 41
  • Same question with provided solution: http://stackoverflow.com/questions/21279882/android-create-an-activity-and-use-it-many-times-with-different-data – kunal.c Oct 17 '14 at 06:42

3 Answers3

1

No, you don't need to create it every time. First, instead of using "add", use "replace". If there is no fragment in fragment manager, your fragment will be added, instead it will be replaced. If you use "add", then you could accidentally add more than one fragment.

You should check for the fragment in the fragment manager and call methods for content updates.

Example:

myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.my_fragment);
if (myFragment == null) {
  myFragment = MyFragment.getInstance(someDataIfNeeded);
  fragmentManager.beginTransaction().replace(R.id.my_fragment, myFragment).commit();
} else {
  myFragment.updateFragmentContent(someData);
}
Thomas R.
  • 7,988
  • 3
  • 30
  • 39
  • You will have problems when you use it this way instead of creating a new fragment passing "someData" over arguments in newInstance(Data someData). When you rotate the screen or your app recovers after a kill (ex. phone call) you will not get your updated data. To have everything on screen you have to do some extra work (onSaveInstanceState etc). Arguments can do everyting for you. – Mark Oct 17 '14 at 06:50
  • Of course. The data in creation is the initial data. I assumed the updated data has been stored in saved bundle :) – Thomas R. Oct 17 '14 at 07:28
1

It depends on your case. In most cases every list item opens a different fragment (with different data). Then you have to make a static newInstance(Data mySerializableData) method in your fragment, use default constructor inside of it, pass data over Fragment arguments See DetailFragment and use fragmentTransaction.replace() in your activity to add this fragment.

When you dont want your fragment to be changed you can create it only once as you say but there is no need of adding it on every item click. So one creation and only one add.

Mark
  • 5,466
  • 3
  • 23
  • 24
1

check instance of that fragment everytime like this-

In your fragment class -

public static final MyFragment newInstance()
{
    MyFragment f = new MyFragment();
    return f;
}

and in your activity when you want to create fragment -

 FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
 fragmentTransaction.add(R.id.root_layout,MyFragment.newInstance());

this is well n good manner...

Rohit Goswami
  • 617
  • 5
  • 17