1

My question is not easy to describe, but I will do my best:

On my tablet-app I have one activity with a listFragment A on left side and a detailFragment B on right side. So when I click an item on the list, the detailFragment shows the proper details of the chosen (list) item.

Now when I click a button on my detailFragment B. the fragment gets swapped with a new Fragment from type infoFragment. The listFragment on left side stays as it is.

So now, when I click another item on the List, I want the infoFragment to vanish and get a detailFragment once again.

My problem is, that i need some kind of check if currently there is an infoFragment or a detailFragment displayed. So that I can either just refresh the detailFragment OR stop the infoFragment and build a new detailFragment.

idea:

 if ( //detailFragment is active ) {
        updateContent();
 }
 else {
        FragmentManager.buildDetailFragment();
 }

have been fiddling for hours now, any help is appreciated!!

How can i figure it out whether there is a detailFragment or listFragment displayed?

edit: i change my detailFragment with the infoFragment here:

FragmentManager fm  = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.details_fragment);

fragment = new InfoFragment();
fm.beginTransaction()
  .replace(R.id.details_fragment, fragment)
  .commit();
bofredo
  • 2,348
  • 6
  • 32
  • 51

4 Answers4

3

When you add a Fragment to your fragment manager with a FragmentTransaction you can specify a key value. You can then findFragmentByTag which will determine if the Fragment with that key value has been added to the fragment manager.

So long as you are not using a ViewPager or some other structure where multple fragments are added at once, searching for whether your fragment manager contains a fragment by tag will let you know which is currently displayed. You can then use the results of that search to update the fragment since the result is a reference to the fragment itself.

This means you can pass data from the FragmentActivity to the fragment directly by calling any publicly accessable fragment methods. For example

Fragment displayedFragment = fragmentManager.findFragmentByTag(TAG);
if(displayedFragment != null){ //null if no fragment with tag value       
    displayedFragment.updateList() //public method within fragment
} 
Rarw
  • 7,645
  • 3
  • 28
  • 46
2
MyFragment myFragment = (MyFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT");
if (myFragment.isVisible()) {
  // add your code here
}

From Here

Community
  • 1
  • 1
Ravi Kumar Karunanithi
  • 2,151
  • 2
  • 19
  • 41
2
FragmentManager fm  = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.content_id);

now we can get the fragment name by getClass

fragment.getClass().getSimpleName()
rfornal
  • 5,072
  • 5
  • 30
  • 42
Shahnawaz Jafri
  • 115
  • 2
  • 7
0

You can get class of fragment and check which one it exactly is by calling getClass().

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141