Android - onBackPressed () not working for me

I have a program. The first action is the splash screen, and the second is the login, and the third is the presentation of the list menu, and then 2 other actions. The splash screen disappears after 3 seconds, and if the โ€œRemember meโ€ checkbox is checked by me, it appears directly on the menu page.
I redefine the onBackPressed function in the menu action so that it onBackPressed program immediately after the user clicks it on the menu. However, if I experienced other activities, he did not go out; it moves to the previous action on the stack, and the dialog box does not appear, although it does appear for a second no less and immediately disappears.

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(); } 
+6
source share
4 answers

I suggest you use the 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 is a tutorial for the same

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

You can use this. However, this is also a bad design. You can check the comments below to find out 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; } 

The Onn back button clicked on the current activity, when you return, click to clear the activity stack and go to MainActivity.

In addition, I would suggest not displaying the warning dialog on the back button. Its a bad design. You can search on SO. I read the same and got a response from commonsware

+7
source

You can undo a reverse button like this

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // write your code here } return true; } 
+1
source
 @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"); } 
+1
source

Removing the finish line (); after each startActivity (); allows you to use the correct navigation instead of calling finish (); which executes the onDestroy () method. Thanks. There is no need to override the onBackPressed function. Use the finish line () only when your activity is no longer needed and needs to be closed.

0
source

Source: https://habr.com/ru/post/945586/


All Articles