0

I am trying to implement onBackPressed on an activity with 3 Fragments. My issue is I implemented a "press again to exit" after some time method however when back button is clicked twice it returns to the previous fragment, not quitting. This happens when that fragment is called (i.e if I open the game and exit straight away it works like a charm). My onBackPressed method on MainActivity looks like this;

@Override
public void onBackPressed() {

    if(getSupportFragmentManager().getBackStackEntryCount()!=0) {
        getSupportFragmentManager().popBackStack();
    }
    this.findViewById(R.id.linearLayout1).setVisibility(View.VISIBLE);
    if(mBackPressed + TIME_INTERVAL > System.currentTimeMillis() && getSupportFragmentManager().getBackStackEntryCount()==0){
        super.onBackPressed();
        System.exit(0);
        this.finish();
    }
    else { Toast.makeText(getBaseContext(), "Press back once more to exit", Toast.LENGTH_SHORT).show(); }

    mBackPressed = System.currentTimeMillis();
}

I basically want to check if the user is in the mainActivity layout so I could evade using popBackStack. Any solution would be appreciated, I could provide more information if this is not enough.

Thanks, Bektaş

Bektaş Şahin
  • 71
  • 2
  • 3
  • 10

3 Answers3

1

If you really want to quit, ignore the back stack altogether. Use this code.

@Override
public void onBackPressed() {
    this.findViewById(R.id.linearLayout1).setVisibility(View.VISIBLE);
    if(mBackPressed + TIME_INTERVAL > System.currentTimeMillis()){
        this.finish();
        return;
    }
    else { 
       Toast.makeText(getBaseContext(), "Press back once more to exit", Toast.LENGTH_SHORT).show(); 
    }
    mBackPressed = System.currentTimeMillis();
}
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • This way I wont have any backpressed action on my fragments. I tried this which leaves me with non back pressable fragments which can be a pain. – Bektaş Şahin May 14 '15 at 23:03
0

I think you might find this link useful http://developer.android.com/training/implementing-navigation/temporal.html

it says that you can add the reference to the stack using this paturn

// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
                           .add(detailFragment, "detail")
                       // Add this transaction to the back stack
                       .addToBackStack()
                       .commit();
wolfaviators
  • 503
  • 1
  • 7
  • 21
  • This is similar to how I go from an activity to a fragment which does not really solve how to hanfle back pressed on fragments and activities. – Bektaş Şahin May 14 '15 at 23:06
-1
@Override
public void onBackPressed() {
    int count = getSupportFragmentManager().getBackStackEntryCount();
    if (count == 0) {
        getSupportFragmentManager().popBackStack(getSupportFragmentManager().getBackStackEntryCount(), getSupportFragmentManager().POP_BACK_STACK_INCLUSIVE);
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
        builder.setCancelable(false);
        builder.setMessage("Are you sure you want to exit?")
                .setNegativeButton(android.R.string.no, null)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        (Main Activity Name).super.onBackPressed();
                    }
                }).create().show();
    } else {
        super.onBackPressed();


    }
}