0

All I want is to exit the application only when back stack is empty.

One section of my app contains a gallery,in which, when a picture is clicked, a fragment showing the full screen image is opened. When back button is pressed in this full screen image, I want to go back to the gallery fragment. I did this by adding the fragment to back stack.

But in other parts of the application, when back button is pressed, I want to show a toast "Please click BACK again to exit". And when the back button is pressed again, the app closes. How can I do this?

This is what I have done so far:

 boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0 ){
       getFragmentManager().popBackStack();
    } else {

        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        commonTasks.ShowStringMessage("Please click BACK again to exit");

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
            }
        }, 2000);

       //super.onBackPressed();
    }
}

Obviously, it didn't work.

Tom Alex
  • 1
  • 2
  • please confirm if you are you adding your fragments to backstack using getFragmentManager() or getSupportFragmentManager() – Nitesh Sep 26 '16 at 12:35
  • @Nitesh: getFragmentManager(). Can you help? – Tom Alex Sep 26 '16 at 12:37
  • your code is looking fine to me.. can you please tell whats happening when your are pressing the back button? – Nitesh Sep 26 '16 at 12:39
  • @Nitesh: Instead of just going back to gallery fragment when I click back button from fullimage fragment, I get the toast message: "Please click BACK again to exit". I have to tap back button twice from full image fragment to go back to the gallery. – Tom Alex Sep 26 '16 at 12:49

3 Answers3

0

You can try using getSupportFragmentManager()

It has a method which returns array of all the fragments it is containing and you can put the check for its size as following

boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getFragments().size() > 0 ){
   getSupportFragmentManager().popBackStack();
} else {

    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    commonTasks.ShowStringMessage("Please click BACK again to exit");

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;
        }
    }, 2000);

   //super.onBackPressed();
}

}

P.S: you also need to change getSupportFragmentManager() while fragment transection.

Also reffer this link Why FragmentManager's getBackStackEntryCount() return zero?

Community
  • 1
  • 1
Nitesh
  • 3,868
  • 1
  • 20
  • 26
0

You can try something diff. Add Exit code in your Activity's onBackPressed Method, and whenever you get the onBackPressed trigger Just check that is your Gallery Fragment is Active? , if it is then Perform Exit code else Change the Fragment.

//define as field variable
private boolean doubleBackToExit = false;

MyGalleryFragment myFragment = (MyGalleryFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT");
if (myFragment != null && myFragment.isVisible()) {

    if (doubleBackToExit) {
        super.onBackPressed();
        finishAffinity();
        return;
    }
    this.doubleBackToExit = true;
    toastMsg(getResources().getString(R.string.touch_again_to_exit));

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doubleBackToExit = false;
        }
    }, 2000);
} else {
    //Change Layout/Fragment
}

Just Dont forget to Add Tag in Fragment...

fragTrans.replace(android.R.id.content, myFragment, "MY_FRAGMENT");

Hope this will work..!

Uttam Panchasara
  • 5,735
  • 5
  • 25
  • 41
  • Have you added Tag with Fragment ? and Added in Activity this code ? – Uttam Panchasara Sep 26 '16 at 14:22
  • Yes I did. But I was getting error in the line: "MyGalleryFragment myFragment = (MyGalleryFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT");" So I did something like this: " Fragment myFragment = getFragmentManager().findFragmentByTag("a");" – Tom Alex Sep 26 '16 at 14:33
  • @TomAlex Here i put that as example you need to use your own fragment name instead of `MyGalleryFragment` and dntt forget to **add Tag.** – Uttam Panchasara Sep 26 '16 at 14:44
0

Try using

super.onBackPressed();

instead of

getFragmentManager().popBackStack();

and let us know about the result.

A. Badakhshan
  • 1,045
  • 7
  • 22
  • The same result. The problem is that control always get into the else section. Even after adding a fragment to the back stack. ie, even after (getFragmentManager().getBackStackEntryCount()>0) is false. Any idea whats going on? – Tom Alex Sep 26 '16 at 14:19
  • What type of activity do you use? Is it AppCompatActivity or FragmentActivity? – A. Badakhshan Sep 27 '16 at 05:47