Pregunta

Ok, so I have 3 activities (A, B, C) Activity A being the main activity. In Activity A, I have a password dialog box and a button (button that leads to activity B).

So, when I open my app, the password dialog box will appear. Now my problem is that, whenever I'm in activity B and I press back button that leads to activity A, the password dialog box keeps appearing. What I want is, the password dialog box will only appear if I open my app and not if I go back to activity A from activity B.

Activity B

public void onBackPressed() {

        AlertDialog.Builder adb = new AlertDialog.Builder(Form.this);
        adb.setTitle("Go back to Activity A?"); 
        adb.setCancelable(false);
        adb.setNegativeButton("Cancel", null);
        adb.setPositiveButton("Exit", new AlertDialog.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                startActivity(new Intent(ActivityB.this, ActivityA.class));
            }
        });
        adb.show();
    }

In my activity B, I have a button (button that leads to activity C). That's why I setup the onBackPressed in activity B.

Activity A

public void toB(View view) {    
        Intent intent = new Intent(this, ActivityB.class);
        startActivity(intent);
}
¿Fue útil?

Solución

It is quite easy. All you have to do when going back to Activity A from Activity B is to tell Activity A that you're coming from Activity B.

In your onBackPressed(), replace startActivity(new Intent(ActivityB.this, ActivityB.class)); with the following code :

Intent i = new Intent(ActivityB.this, ActivityB.class);
i.putExtra("from", "activityB");

Then, in Activity A, just check for this extra before displaying the dialog box :

Intent thisIntent = getIntent();
if (thisIntent != null && thisIntent.getExtras() != null
        && thisIntent.getExtras().containsKey("from")
        && thisIntent.getExtras().getString("from").equals("activityB")) {
        // Coming from Activity B : nothing
} else {
    // Display your dialog box
}

Otros consejos

You can simply use a flag through the bundlesavedinstance to notify activity A that it's being called from activity B

Even though you've accepted an answer, here are some points to keep in mind.

Your navigation stacks might be:

  • A -> B -> pressing Back -> A
  • A -> B -> C -> pressing Back -> B -> pressing Back -> A

Why don't let Android handle this for you? How? Because you're not finishing A, its' onPause() is called when you start B. Now in B, when pressing Back button, just call finish(), instead of using an Intent to go to A. That way, provided that your password dialog is in onCreate() in A, A's onResume() will be called and dialog won't be displayed.

public void onBackPressed() {
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    adb.setTitle("Go back to Activity A?"); 
    adb.setCancelable(false);
    adb.setNegativeButton("Cancel", null);
    adb.setPositiveButton("Exit", new AlertDialog.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            //calling finish() will close this Activity(B) and go back to the previous
            //which in your case is Activity A if B was called from A
            finish();
        }
    });
    adb.show();
}

just add dialog.dismiss(); in your dialog box. that will solve your problem.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top