Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - onBackPressed() not working for me

Tags:

android

I have a program. The first activity is a splash screen and the second is the log in and the third is a list view menu activity, and then 2 other activities. The splash screen disappear after 3 seconds and if the log in check box remember me is checked, it goes directly to the menu page.
I override the onBackPressed function in the menu activity so that it will exit the program directly after the user click back from the menu. However, if I have gone through the other activities it doesn't exit; it goes to the previous activity in the stack and the dialog box doesn't pop up, although it actually does appear for a second no less and disappear right away.

Here is my onBackPressed function

public void onBackPressed() {
    // super.onBackPressed();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you Sure want to close the Application..?")
        .setCancelable(false)
        .setTitle("EXIT")
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        })
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                finish();
            }
        });
    AlertDialog alert = builder.create();
    alert.show();
    //super.onBackPressed();
}
like image 742
user2401745 Avatar asked May 22 '13 12:05

user2401745


2 Answers

I would suggest you to use ActionBar as suggested by WarrenFaith in the comments below. Pls check the link below for more information

http://developer.android.com/design/patterns/navigation.html

Here's a tutorial for the same

http://www.vogella.com/articles/AndroidActionBar/article.html

You can use this. However this also seems to be a bad design. You can check the comments below to know why

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();
    }
    return super.onKeyDown(keyCode, event);
}

public void onBackPressed() {
    Intent myIntent = new Intent(MyActivity.this, MainActivity.class);

    myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(myIntent);
    finish();
    return;
}

Onn back button pressed inn your current activity when you back press you clear the activity stack and naviagate to MainActivity.

Also i would suggest no to display a alert dialog on back button press. Its a bad design. You can search on SO. i read the same and was answered by commonsware

like image 107
Raghunandan Avatar answered Nov 15 '22 21:11

Raghunandan


@Override
public void onBackPressed() {
    super.onBackPressed();
    int count = getSupportFragmentManager().getBackStackEntryCount();
    Log.e("count", "is getBackStackEntryCount -> " + count);
    for (int i = 0; i < count; ++i) {
        getSupportFragmentManager().popBackStackImmediate();
        Log.e("getBackStack", "IS REMOVED " + i);
    }
    // toolbar.setTitle("Dashboard");
}
like image 33
Keshav Gera Avatar answered Nov 15 '22 21:11

Keshav Gera