Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

android - onBackPressed() not working for me

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...