Ask the user to exit the application on the rear drive in android?

I have a list of actions in my application.

If the user is on a home operation, the message “Ask the user to press again to exit” appears.

But if the user goes to the 2nd or 3rd action and returns to home activity again, my code cannot show a toast.

I want that every time a user at a home operation, a toast should appear.

I know there is some kind of error in my logic. Can someone help me please.

This is the code to push back.

    @Override
    public void onBackPressed() {
    i++;
    if (i == 1) {
        Toast.makeText(HomeActivity.this, "Press back once more to exit.",
                Toast.LENGTH_SHORT).show();
    } else if(i>1) {
        finish();
        super.onBackPressed();
    }
}
+4
source share
6 answers

You must establish i = 0if you return to HomeActivity.

, HomeActivity.java i = 0 OnResume()

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    i = 0;
} 
+5

, . , 3 . , 3 , .

private boolean exit = false;
@Override
public void onBackPressed() {
    if (exit)
        Home.this.finish();
    else {
        Toast.makeText(this, "Press Back again to Exit.",
                Toast.LENGTH_SHORT).show();
        exit = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                exit = false;
            }
        }, 3 * 1000);

    }

}
+8

, Activity, i=0 startActivity(xyz) HomeActivity. , , .

+1

AlertDialogBox in onBackPressed homeActivity.. finish() Yes.

  @Override
public void onBackPressed()
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Exit");
    builder.setMessage("Are You Sure?");

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
            }
        });

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}
0

. :

int i = 1;


@Override
        public void onBackPressed() {       
            if (i == 1) {
                Toast.makeText(getApplicationContext(), "Press back once more to exit.",
                        Toast.LENGTH_SHORT).show();


            } else if(i>1) {
                finish();
            }
            i++;
        }
0
source

Logics:

  • Create a variable with an initial value as 0

  • set the value to 1 after showing the toast, so that the value can be entered if the operator is pressed again

  • create a handler with the specified delay so that if the user does not click back on the specified one, he changes the value to 0, which prevents the IF condition from being entered.

\ int x = 0;

 @Override
 public void onBackPressed() {

    if(x==1) {
        super.onBackPressed();
        return;
    }
    Toast.makeText(this,"Press Again to Exit!!",Toast.LENGTH_SHORT).show();
    x=1;

    android.os.Handler handler=new android.os.Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            x = 0;
        }
    }, 2000);  //2 sec delay

}
0
source

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


All Articles