I have an activity with fragments that it navigates too. When you hit back on the fragment it should return back to the activity. But when you hit back at the moment the app closes.

Now thats not totally undesired behaviour as I would like the app to close when you hit back on the main activity.

Code on main activity that does this:

```
@Override
    public void onBackPressed() {
        DrawerLayout drawer = findViewById(R.id.drawer_layout);

        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();

            this.finishAffinity();
        }
    }

```

There is no onBackPressed method on the fragment

How can I make this so that when you hit back on the fragment the main activity shows but when you hit back on the activity the app closes