Bindl
Bindl

Reputation: 81

Where to add code for onBackPressed() for Fragements

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

Upvotes: 1

Views: 65

Answers (1)

Shrey Gupta
Shrey Gupta

Reputation: 392

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();
}
}

Upvotes: 0

Related Questions