Siju
Siju

Reputation: 2655

Onbackpressed in fragment not working while implementing interface

I am trying to override onbackpressed method inside fragment . But it gives me syntax error that it should override a super method in interface. Why so? I have other methods like ondestroy also in my fragment class but no error. Why for this backpressed alone. I tried onkeydown also. Same error. Pasting below my code.

  public TestClass extends Fragment implements    Testinterface
{  @Override //error must override or implement    supertype method
 public void onBackPressed ()
{
 if (check)
Do somethin
 else
  getActivity().finish ()
  //super.onBackPressed () // error here if I use this
  }

Upvotes: 5

Views: 27952

Answers (3)

Daniel Park
Daniel Park

Reputation: 288

Try to avoid using onKey in fragment. There is a better way. For maintenance, I recommend you to use getBackStackEntryCount()

in Activity

final FragmentMananger fm = new getSupportFragmentManager(); 

@Override
protected void onCreate(Bundle b){
   super.onCreate(b);
   setContentView(R.layout.activity_main);

   Fragment fragment = new SomeFragment();
   // if you don't run on prior to Android 3.0 use getFragmentManager();
   fm.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack(null).commit();
   // R.id.frame_container is the id of FrameLayout in activity_main.xml
}

@Override
public void onBackPressed(){
   if(fm.getBackStackEntryCount() == 0){
       finish();
   }else{
    super.onBackPressed();
   }
}

Upvotes: 2

franmontiel
franmontiel

Reputation: 1910

You can propagate onBackPressed() to all your fragments,for that you need to create two classes with the following methods and then make all your activties and fragments inherit from them:

public class BaseActivity extends ActionBarActivity {

    @Override
    public void onBackPressed() {
        boolean eventConsumed = false;
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                if (fragment instanceof BaseFragment) {
                    eventConsumed = eventConsumed
                            || ((BaseFragment) fragment).onBackPressed();
                }
            }
        }
        if (!eventConsumed) {
            super.onBackPressed();
        }
    }
}

public class BaseFragment extends Fragment {

    public boolean onBackPressed() {
        return false;
    }
}

Note that this code is using the support library, if you are not using it you need to do the appropriate changes.

Upvotes: 4

Junaid
Junaid

Reputation: 7860

You have to implement on key down in fragment, check for key code. The onBackPress() method can be used in an Activity -- which is the logical parent of your fragment.

Try this:

frag.getView().setFocusableInTouchMode(true);
frag.getView().setOnKeyListener( new OnKeyListener(){
    @Override
    public boolean onKey( View v, int keyCode, KeyEvent event ){
        if( keyCode == KeyEvent.KEYCODE_BACK ){
            return true;
        }
        return false;
    }
} );

Upvotes: 12

Related Questions