Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 838
alois.wirkes Avatar asked Apr 11 '13 14:04

alois.wirkes


2 Answers

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.

like image 72
Jay Snayder Avatar answered Oct 15 '22 20:10

Jay Snayder


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

like image 37
codeMagic Avatar answered Oct 15 '22 18:10

codeMagic