2

I've had to use instanceofseveral 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?

u3l
  • 3,342
  • 4
  • 34
  • 51
  • The typical way of bypassing the need for the `instanceof` operator is the `Visitor pattern` – EpicPandaForce Jul 03 '14 at 12:51
  • 2
    Looks to me like `CustFragmentOne`, `CustFragmentTwo`, `CustFragmentThree` and `CustFragmentFour` should all extend an interface that has some method to identify if the object needs to display the value. – Dan Temple Jul 03 '14 at 12:53

6 Answers6

2

The simplest way is to have CustFragmentOne, CustFragmentTwo, etc. implement a common interface. That way you just need to do a single instanceof check against that interface, instead of needing to check each concrete class individually.

Another way would be something like along these lines: How to implement onBackPressed() in Fragments?

Community
  • 1
  • 1
vaughandroid
  • 4,315
  • 1
  • 27
  • 33
  • Got it... Also I've looked at that link before, but I find it pretty confusing, is there any simpler explanation you know of? – u3l Jul 03 '14 at 12:54
2

Use an interface. For example:

public interface ICustomFragment {

      public boolean shouldNavigateBack();

And then you can do something like this:

@Override
public void onBackPressed() {

CustomFragment frag = (CustomFragment)mManager.findFragmentByTag("Fragment");

    if(frag.shouldNavigateBack()) {

            //This method displays an instance of "CustFragmentFive"
            displayView(0);
    }
    else {
         super.onBackPressed();
}
OferM
  • 902
  • 1
  • 8
  • 20
0

if(frag.getClass() == CustFragmentOne.class) not sure which approach is better tho

j.con
  • 879
  • 7
  • 19
0

Why dont you keep your Fragment objects into your Activity?

CustFragmentOne fragment1 = (FragmentOne)getFragmentManager().findFragmentById(R.id.fragment1);  
 CustFragmentTwo fragment2 = (FragmentOne)getFragmentManager().findFragmentById(R.id.fragment2);

and in your layout xml file you will have smth like this:

<FrameLayout
    ...>
    <fragment
        android:id="@+id/fragment1"
        android:name="com.smartmirror.FragmentOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <fragment
        android:id="@+id/fragment2"
        android:name="com.smartmirror.FragmentTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</FrameLayout>
Ispas Claudiu
  • 1,890
  • 2
  • 28
  • 54
0

I suggest you to use something like this to have control of the back button on the fragments:

  1. Create a base fragment abstract class:

    public abstract class BaseFragment extends Fragment {
    
        public void onBackPressed() {
    
        }
    
    }
    
  2. Extend all your fragments from BaseFragment

    public class CustFragmentOne extends BaseFragment { ... }

  3. Change your activity

    @Override
    public void onBackPressed() {
    
        Fragment frag = mManager.findFragmentByTag("Fragment");
    
        if(frag instanceof BaseFragment)
            frag.onBackPressed();
        else if(mManager.getBackStackEntryCount() > 0)
            getFragmentManager().popBackStack();
        else
           super.onBackPressed();
    
    }
    
  4. override in your fragments the onBackPressed() method with the logic you want.

jonyjm
  • 963
  • 2
  • 7
  • 15
0

You could always create a BaseFragment that extends Fragment to be extend by all fragments. Then you can let each fragment handle it as necessary making it a bit cleaner. e.g.

public class BaseFragment extends Fragment { public void onBackPressed() { // Override this function in your fragment classes as needed } }

or you could do something like (Android Fragment handle back button press)

Community
  • 1
  • 1
Bruce
  • 306
  • 2
  • 7