Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if 2 fragments have an instance from the same class

Let's say that I have a fragment inside a container that I should replace with an other fragment.

The code is quite simple:

getSupportFragmentManager().beginTransaction().
replace(R.id.content_frame,fragmentToAdd).addToBackStack(null).commit();

The problem is now that I want to make sure that fragmentToAdd is not similar to the fragment I will replace.

If this is the case, the app should do nothing.

So, I can get the original Fragment:

Fragment originalFragment=(Fragment) getSupportFragmentManager().findFragmentById(R.id.content_frame);

and the final code would be something like:

if (haveDifferentInstance(originalFragment,fragmentToAdd)){
    getSupportFragmentManager().beginTransaction().
    replace(R.id.content_frame,fragmentToAdd).addToBackStack(null).commit();}
else
    // Do nothing

So, what could be the code for haveDifferentInstance(Fragment fragmentA, Fragment fragmentB)?

Thank for any help.

EDIT

I know that I can do somethinh like:

if (fragmentToAdd instanceof FragmentA)

But has I have FragmentA, FragmentB, .... Fragment F, I don't want to use a 'if' per type of Fragment.

like image 284
Waza_Be Avatar asked Nov 12 '12 21:11

Waza_Be


1 Answers

Check if the class are equal : if(origianalFragment.getClass().equals(fragmentToAdd.getClass()))

like image 60
yDelouis Avatar answered Nov 05 '22 17:11

yDelouis