How do handle onBackPressed() in Fragment?

Hardik Thummar picture Hardik Thummar · Aug 9, 2016 · Viewed 18.2k times · Source

This is my class name

public class PrimaryFragment extends Fragment implements OnRestartRequest {

    @Override
    public void onBackPressed() {
        if (mCardStackLayout.isCardSelected()) {
            mCardStackLayout.restoreCards();
        } else {
            super.onBackPressed();
        }
    }

Error on this line: super.onBackPressed();

I don't know what's the problem in fragment class's on back pressed button.

Answer

Smit Davda picture Smit Davda · Aug 9, 2016

There is no onBackPressed() method in Fragment. You can do something like calling getActivity().onBackPressed() from your Fragment.