Since question is quite similar but I didn't found an exact answer to it. I tried the answer on the link below but it is not working!!
Android Double Back Press to close the app having fragments
Here are my codes:
public interface OnBackPressedListener {
void onBackPressed();
}
Now the fragment:
public class HomeFragment extends Fragment implements View.OnClickListener, OnBackPressedListener{
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
//Checking for fragment count on backstack
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else if (!doubleBackToExitPressedOnce) {
this.doubleBackToExitPressedOnce = true;
Toast.makeText(getActivity(),"Tap again to exit.", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
}
Tried using getSupportFragmentManager() as well, but it is highlighted red. I need to double tap from the fragment (a bottom navigation bar in android) to exit from application.