Search code examples
androidback-button

android go back to previous view


I am working on a android application and i want to exit the application on back press when it comes to state where no view is available to go back. Until then it should go to the previous view on back press. How can i do this.

What i need

  • Go back to previous view on back press.
  • If you cant go back anymore show a toast and ask to exit the application.

my code

@Override
    public void onBackPressed() {



            DialogInterface.OnClickListener dialogInterface = new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:


                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                        finish();

                        break;

                    case DialogInterface.BUTTON_NEGATIVE:


                    }

                    commonVariable.setSteps(0);

                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(
                    MainActivity.this);
            builder.setMessage("Are you sure to exit the app?")
                    .setPositiveButton("Yes", dialogInterface)
                    .setNegativeButton("No", dialogInterface).show();



        }
    }

Solution

  • SOLUTION

    @Override 
        public void onBackPressed() { 
            if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                getSupportFragmentManager().popBackStack(); 
            } else { 
                DialogInterface.OnClickListener dialogInterface = new DialogInterface.OnClickListener() {
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
    
    
                            Intent intent = new Intent(Intent.ACTION_MAIN);
                            intent.addCategory(Intent.CATEGORY_HOME);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            finish();
    
                            break;
    
                        case DialogInterface.BUTTON_NEGATIVE:
    
    
                        }
    
                        commonVariable.setSteps(0);
    
                    }
                };
    
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this);
                builder.setMessage("Are you sure to exit the app?")
                        .setPositiveButton("Yes", dialogInterface)
                        .setNegativeButton("No", dialogInterface).show();
            }
        }