0

I have simple method where I want to pass fragment name as parameter. I have few fragments like

1) AddNewDatesFragment

2) AskFragment

3) DisclaimerFragment

I have below code that work properly as expected

FragmentManager fragmentManager = activity.getSupportFragmentManager();
    for (int entry = 0; entry < fragmentManager.getBackStackEntryCount();     entry++) {
    Fragment fragment = (Fragment)    fragmentManager.getBackStackEntryAt(entry);
        if (fragment instanceof AddNewDatesFragment) {
            Log.i("TAG", " Expected Fragment Found");
        }
    }

I want to write some code in method where I could call methods like this

        isExistFragment(AddNewDatesFragment);
        isExistFragment(AskFragment);
        isExistFragment(DisclaimerFragment);

etc...

I have tried code like this but not working gives compile time error at if condition.

    private boolean isExistFragment(Class<Fragment> expectedFragment) {
    FragmentManager fragmentManager = activity.getSupportFragmentManager();
    for (int entry = 0; entry < fragmentManager.getBackStackEntryCount(); entry++) {
        Fragment fragment = (Fragment) fragmentManager.getBackStackEntryAt(entry);
        if (fragment instanceof expectedFragment) {
            Log.i("TAG", " Expected Fragment Found");
            return true;
        }
    }
    return false;
}

I am curious about implementing such method. Thanks,

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
khurram
  • 1,362
  • 13
  • 24
  • compile time error at which line in the flowing code.. – KomalG Jan 02 '15 at 13:09
  • at if condition as : Multiple markers at this line - expectedFragment cannot be resolved to a type - Incompatible conditional operand types Fragment and expectedFragment – khurram Jan 02 '15 at 13:11
  • Fragment Return null : in this line : Fragment fragment = (Fragment) fragmentManager .findFragmentById(fragmentManager.getBackStackEntryAt(entry) .getId()); – khurram Jan 02 '15 at 13:40

4 Answers4

1
getBackStackEntryAt(index) 

does not return Fragment itself, it return BackStackEntry object for the respected fragment.

Please have a look http://developer.android.com/reference/android/app/FragmentManager.html#getBackStackEntryAt(int)

Use below snippet to retrieve Fragment from the stack:

   Fragment fragment = (Fragment) fragmentManager
            .findFragmentById(fragmentManager.getBackStackEntryAt(entry)
                    .getId());

Here, first you get BackStackEntry for the respected Fragement, by which you would get fragment id of fragment at perticular location. Then by id you easily can retrieve Fragment.

Anand Tiwari
  • 1,583
  • 10
  • 22
0

You could maybe use:

expectedFragment.isAssignableFVrom(fragment.getClass())

This will be true if the exceptedFragment class is an interface or superclass of the fragment class (see doc).

(PS: you can not use fragment instanceof expectedFragment on exceptedFragment because it is a class and not an object.)

Nemolovich
  • 391
  • 2
  • 12
0

i think the problem with the import statements in the class extends FragmentActivity you imported supportv4 and in that expectedFragment you imported "import android.app.Fragment" once check import statements.

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

or import android.app.Fragment;

KomalG
  • 808
  • 9
  • 18
0

I don't know whether it is the best way or not. But I would use tag feature for the similar purpose.

When you add a fragment set a tag for later identification:

fragmentManager.beginTransaction()
               .replace(R.id.container, newlyAddFragment, TAG_FOR_THE_FRAGMENT)
               .commit();

And implement isExistFragment like this:

boolean isExistFragment(String tag) {
    Fragment targetFragment = fragmentManager.findFragmentByTag(tag);
    return targetFragment != null;
}
hata
  • 11,633
  • 6
  • 46
  • 69