0

I added a fragment inside an activity by using the following code:

FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.parent_fragment_container, new FolderStructureFragment());
ft.commit();

My question is how can I get the reference of the added fragment. I have searched a lot of key words related to fragment and FragmentTransaction but didn't find anything close to my requirement which I think is a very basic function and should be offered to us. Any help is much appreciated!

MPhil
  • 881
  • 1
  • 12
  • 24
user1870797
  • 121
  • 1
  • 14
  • Possible duplicate of [Get the current fragment object](https://stackoverflow.com/questions/6750069/get-the-current-fragment-object) – Iulian Popescu Jul 28 '17 at 09:25

4 Answers4

3

There are the two major possiblities:

  • You can remember the reference by yourself
  • By an Tag
  • By ID

For example (nearly the same for by ID):

ft.replace(R.id.fragment_container, fragment, tagOfFragment);
getSupportFragmentManager().findFragmentByTag(tagOfFragment);
MPhil
  • 881
  • 1
  • 12
  • 24
  • This does not work for me. It might be that I create and operate the fragment inside another fragment instead of an activity. – user1870797 Jul 26 '17 at 01:12
  • You should be able to find the fragment by the possibilites 2 and 3, no matter where you create it. Just call findFragmentByTag from the SupportFragmentManager. – MPhil Jul 26 '17 at 05:08
  • i am always returned with null by calling getSupportFragmentManager().findFragmentByTag(tagOfFragment) or findFragmentByID(IDOfFragment); I even tried getChildFragmentManager, but still no different. – user1870797 Jul 26 '17 at 05:17
  • Try this: https://stackoverflow.com/questions/23581894/getfragmentmanager-findfragmentbytag-returns-null – MPhil Jul 26 '17 at 05:20
  • Maybe I should try to test your approach in an activity to make sure this is not caused by doing it inside another fragment. – user1870797 Jul 26 '17 at 05:28
  • This sounds possible to work out. But I just found that I could retrieve the fragment by calling findFragmentByID now. I can do this in an onClick event. So I guess the reason I failed earlier was that it will take a little while to create and insert the fragment inside the parent ViewGroup so an immediate call to findFragmentByID following FragmentTransaction.commit is not able to find anything. – user1870797 Jul 26 '17 at 09:19
  • getSupportFragmentManager().executePendingTranscation(); could help to get it immediatelly – MPhil Jul 26 '17 at 09:21
  • Yes, I think this is the right function I should take. – user1870797 Jul 26 '17 at 09:24
1

try using the below code.

FolderStructureFragment folderStructureFragment = (FolderStructureFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.parent_fragment_container);
taug
  • 378
  • 1
  • 8
  • 1
    try using `getChildFragmentManager()` to add or replace fragments inside another fragment instead of `getActivity().getSupportFragmentManager()` – taug Jul 26 '17 at 06:09
  • 1
    That's sad. :( You can keep the reference of the current fragment in another way. Create the fragment objects and store it in a `Fragment` class object. And in `replace` function use that `Fragment` class object instead of `new`-ing. So you will always have a reference to the currently loaded fragment. It's bit complicated solution, but it will get the work done. – taug Jul 26 '17 at 09:00
  • This sounds possible to work out. But I just found that I could retrieve the fragment by calling findFragmentByID now. I can do this in an onClick event. So I guess the reason I failed earlier was that it will take a little while to create and insert the fragment inside the parent ViewGroup so an immediate call to findFragmentByID following FragmentTransaction.commit is not able to find anything. – user1870797 Jul 26 '17 at 09:19
  • Thanks. I really appreciate your help:) – user1870797 Jul 26 '17 at 09:26
0
    FolderStructureFragment folderStructureFragment = new FolderStructureFragment();
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.parent_fragment_container, folderStructureFragment);
    ft.commit();

Here , folderStructureFragment is reference for newly added fragment.

karanatwal.github.io
  • 3,613
  • 3
  • 25
  • 57
  • In my case, folderStructureFragment is not initialized with activity resources. Its just a simple new class rather than being a part of its parent activity. – user1870797 Jul 26 '17 at 01:16
  • Ok if it is a class it can be initialised this way and then `folderStructureFragment` can be used anywhere in your current activity as a reference to `FolderStructureFragment` class. Give a try. – karanatwal.github.io Jul 26 '17 at 05:34
  • 1
    This sounds possible to work out. But I just found that I could retrieve the fragment by calling findFragmentByID now. I can do this in an onClick event. So I guess the reason I failed earlier was that it will take a little while to create and insert the fragment inside the parent ViewGroup so an immediate call to findFragmentByID following FragmentTransaction.commit is not able to find anything. – user1870797 Jul 26 '17 at 09:20
0

There is an easy way to get a reference to a fragment any time you need, and that is with a TAG. Whenever you add a fragment you may give it a specific tag, and you may retrieve it anytime you want with that same TAG.

This is how you add it:

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.fragment_container, myFragment,"myFragmentTag").commit();

Now let's say, somewhere in the code you need the reference to this fragment, you only need to call the following

MyFragment myFragment = (MyFragment)getSupportFragmentManager().findFragmentByTag("myFragmentTag");
if(myFragment != null && myFragment.isAdded()){ //you should always do this check when you retrieve fragment instances
    //do whatever you need with it
}
Ricardo
  • 9,136
  • 3
  • 29
  • 35
  • No luck for me. It might be that I create and operate the fragment inside another fragment but an activity. – user1870797 Jul 26 '17 at 01:21
  • That does not make a difference if you cast the fragment manager from the activity, i.e: `getActivity().getSupportFragmentManager()` – Ricardo Jul 26 '17 at 08:17
  • This sounds possible to work out. But I just found that I could retrieve the fragment by calling findFragmentByID now. I can do this in an onClick event. So I guess the reason I failed earlier was that it will take a little while to create and insert the fragment inside the parent ViewGroup so an immediate call to findFragmentByID following FragmentTransaction.commit is not able to find anything. – user1870797 Jul 26 '17 at 09:19
  • Check out my answer here https://stackoverflow.com/questions/43520688/findfragmentbyid-and-findfragmentbytag – Ricardo Jul 26 '17 at 10:49