alois.wirkes
alois.wirkes

Reputation: 369

Calling onResume method after an alert is dismissed in Android?

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!

Upvotes: 10

Views: 23742

Answers (4)

Jay Snayder
Jay Snayder

Reputation: 4338

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.

Upvotes: 21

codeMagic
codeMagic

Reputation: 44571

To see if a method is called you can put a breakpoint at the method, onResume(), to see what happens. If you aren't familiar with the Acitvity Lifecycle then doing this will help you familiarize yourself with it and reading the provided documentation.

Now, I don't think you should redraw your whole layout just to clear some Views. It would be more efficient, in my opinion, to just reset all fields by using setText() or another method for whatever you need when the user clicks "ok " or whatever. You can use invalidate() if you need to redraw certain Views

I also recommend watching

Google I/O-Turbo Charge Your UI

Activity LifeCycle <-- very important to understand

Upvotes: 1

L. G.
L. G.

Reputation: 9761

If you're not sure when the onResume is called, add a log into the onResume method.

Upvotes: 0

TronicZomB
TronicZomB

Reputation: 8747

AFAIK This is not possible since in order to have displayed the dialog box the activity would have already passed the onResume state. Check out the following page for more on the life cycle on an Android app (it really helped me understand better):

App lifecycle

Upvotes: 0

Related Questions