I've had to use instanceof
several times here while checking what subclass a Fragment
is, and I feel like I'm going wrong somewhere:
@Override
public void onBackPressed() {
Fragment frag = mManager.findFragmentByTag("Fragment");
if(frag instanceof CustFragmentOne ||
frag instanceof CustFragmentTwo ||
frag instanceof CustFragmentThree ||
frag instanceof CustFragmentFour) {
//This method displays an instance of "CustFragmentFive"
displayView(0);
}
else if(frag instanceof CustFragmentFive)
super.onBackPressed();
else if(mManager.getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
else
super.onBackPressed();
}
Is there any way around using it here? How else can I identify the fragment's type and accordingly invoke the right method?