Calling onResume method after an alert is dismissed in Android?

alois.wirkes picture alois.wirkes · Apr 11, 2013 · Viewed 20.8k times · Source

I want to know if when the user presses "Yes" on an alert dialog and this one is dismissed, that event executes the onResume method of the activity in which the user is in.

Because I have a "Clean" button that asks the user if he's really sure of cleaning all the fields of the form (the activity) in order to redraw the activity with the empty fields.. The form is created dynamically, so I don't know a priori the elements in the GUI to set them empty...

Sorry for my bad english!!

Thanks and Greetings!

Answer

Jay Snayder picture Jay Snayder · Apr 11, 2013

Not sure if this is the approach that should be taken, but you should be able to do what I think you are requesting. If for some reason this is what you would want to achieve.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to clean?")
      .setPositiveButton("Yes", new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface dialog, int id)
        {
          dialog.dismiss();
          ((ActivityName) appContext).onResume();
        }
      })
      .setNegativeButton("No", new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface dialog, int id)
        {
          dialog.dismiss();
        }
      });
    builder.create().show();
}

You will really want to be calling your clean function instead of anything like a lifecycle call on a success while doing nothing on a failure.

Another potential way to approach this would be to bring the current activity back to the front using flags.

Intent intent = new Intent(this, CurrentlyRunningActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

Which would also provide a way to call upon your main activity without directly referencing the onResume() call, as pointed out is not the appropriate approach; however, I did want to directly respond to the question as it was asked.