user8510560
user8510560

Reputation:

How to use onBackPress in Fragment

In my application I use ViewPager for show two fragments in an activity. In one of the fragments I use NavigationDrawer. I want when click on onBackPress close this NavigationDrawer.

I wrote below code for open this Drawer :

reviewSerialFrag_DrawerLayout.openDrawer(Gravity.END);

I want that when I click on onBackPress it will close this drawer with below code:

reviewSerialFrag_DrawerLayout.closeDrawer(Gravity.END);

questioner, put in what the current problem is, please

Upvotes: 0

Views: 296

Answers (5)

TonnyL
TonnyL

Reputation: 1345

According to the AndroidX release notes, androidx.activity 1.0.0-alpha01 is released and introduces ComponentActivity, a new base class of the existing FragmentActivity and AppCompatActivity. And this release brings us a new feature:

You can now register an OnBackPressedCallback via addOnBackPressedCallback to receive onBackPressed() callbacks without needing to override the method in your activity.

Upvotes: 1

Dmitriy Mitiai
Dmitriy Mitiai

Reputation: 1200

You can keep the flag that drawer was opened. And when you override the onBackPressed(), check this flag and if it's true, call

reviewSerialFrag_DrawerLayout.closeDrawer(Gravity.END);

otherwise, call super.onBackPressed() or any other logic you need.

Upvotes: 0

Yuriy Pelipas
Yuriy Pelipas

Reputation: 21

My solution:
Create BaseFragment.java:

public class BaseFragment extends Fragment
    {
        public boolean onBackPressed()
        {
             return false;
        }
    }

then extend your fragment by this BaseFragment, and in activity:

@Override
public void onBackPressed()
{
    if (!yourFragment.onBackPressed())
        super.onBackPressed();
}

In yourFragment:

public class YourFragment extends BaseFragmnet
{

    ...
    .
    .

    @Override
    public boolean onBackPressed()
    {
        // do something...

        return true; // you should return true;
    }
}

Upvotes: 1

Maxime Jallu
Maxime Jallu

Reputation: 2382

The best solution is create your interface, and implement in Fragment.

Solution here : implement onBackpress in Fragment

public interface IOnBackPressed {
    /**
     * Si vous retouné true le back press ne sera pas pris en compte, sinon l'activité agira naturellement
     * @return true si votre traitement est prioritaire sinon false
     */
    boolean onBackPressed();
}

see link for more details ... easy sample

Upvotes: 0

Bhuvanesh BS
Bhuvanesh BS

Reputation: 13617

onBackpress() only called in fragment if you need back press event in fragment you have to implement interface to get onBackPress() callback.

In Activity:

public MyActivity extends AppCompatActivity{

private BackPressListener backPressListener;

    @Override
    public void onBackPressed() {
        if (backPressListener != null) {
            backPressListener.onActivityBackPress();
        } else {
            super.onBackPressed();
        }
    }

    public void setBackPressListener(BackPressListener backPressListener) {
        this.backPressListener = backPressListener;
    }

    public interface BackPressListener{
        void onActivityBackPress();
    }

}

In Fragment:

public class MyFragment extends Fragment implements BackPressListener{

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MyActivity)getActivity()).setBackPressListener(this);
    }

    @Override
    public void onActivityBackPress() {
        // handle your back press here.

       reviewSerialFrag_DrawerLayout.closeDrawer(Gravity.END);

       getActivity().onBackPressed();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((MyActivity)getActivity()).setBackPressListener(null);
    }
}

Upvotes: 1

Related Questions