i`m having an app with only one activity but 3 fragments.
Now i found a way (Youtube Video) to stop the app to exit complete after hitting the back button on my phone.
boolean twice = false;
@Override
public void onBackPressed() {
Log.d(TAG, "click");
if(twice == true){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
twice = true;
Log.d(TAG, "twice: " + twice);
// super.onBackPressed();
Toast.makeText(MainActivity.this, "Tap twice to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
twice = false;
Log.d(TAG, "twice: " + twice);
}
}, 2000);
}
Another Thread shows code to "navigate" between fragments with the back button How to implement onBackPressed() in Fragments?
That`s the code
@Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
if (count == 0) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}
}
When i add under the first code lines the app exit complete after hitting the back button on the phone. Can someone tell me where to add the second code lines so that both things work?
Thanks a lot in advance