1

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

Bindl
  • 81
  • 2
  • 6

1 Answers1

0

UPDATED Here Is What I Have Done While Fragment Transaction To Add Fragment Entry to BackStack

ReviewOrder fragment = new ReviewOrder();
                FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.slide_in_up,  
                        R.anim.slide_out_up); //OPTIONAL For Animations
                fragmentTransaction.replace(R.id.tabframelayout,fragment,FRAGMENT_TAG);

                //Add This To Your Code To Add To BackStack
                fragmentTransaction.addToBackStack(FRAGMENT_TAG);


                fragmentTransaction.commit();

What You can do is execute your double tap logic (First code) only when (count == 0) so your app won't exit on back press if there is a fragment in the backstack

boolean twice = false;
@Override
public void onBackPressed() {

int count = getFragmentManager().getBackStackEntryCount();

if (count == 0) {
    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);
        super.onBackPressed();
    }
    twice = true;
    Log.d(TAG, "twice: " + twice);

    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);
} else {
    getFragmentManager().popBackStack();
}
}
Shrey Gupta
  • 392
  • 4
  • 10
  • unfortunately it always returns 0, so it always says "Tap twice to exit". it never "jumps" back to the last fragment. Do you need any more details what i`m using, let me know – Bindl Jan 12 '18 at 23:55
  • Have A Look At The Updated Answer – Shrey Gupta Jan 13 '18 at 08:12
  • I do have one last question. I don`t know where to add "ReviewOrder". Do i have to add it in the MainActivity.java or in StandortFragment.java? So in each Fragment or in the MainActivity.java? – Bindl Jan 13 '18 at 11:27
  • ReviewOrder will be replaced by your fragment – Shrey Gupta Jan 13 '18 at 11:40