简体   繁体   中英

Update listview in main activity when alert dialog of another activity gets dismissed

I have a dialog activity which sets up an alert dialog every time the alarm goes off. Now this alert dialog can be infront of any activity. As it gets called by the alarm manager when the alarm is supposed to go off. The problem is that if this alert dialog comes infront of the main activity which holds the list of upcoming alarms then when I "dismiss" the alarm, that alarm should get removed from the list( which does get deleted) but after the alert dialog disappears the main activity doesn't get updated unless I resume the activity or go to next activity and then back to main activity.

I tried calling the main activity inside the onDismiss method of the dialog like this:

 alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {

                ((MainActivity) context).customAdapter.notifyDataSetChanged();
        }
    });

However I get an exception that my DialogActivity's context can't get converted into main activity's context. How can I solve this?

Here's my Dialog Activity class

public class DialogActivity extends AppCompatActivity {

AlarmManager alarmManager;
PendingIntent pendingIntent;
Intent startSetAlarm;
Intent i;
Intent intent;
String alarmStatus;
int alarmID;
String alarmName;
String alarmTime;
String alarmAMPM;
String alarmSound;
String snoozingType;
Context context;

private SensorManager mSensorManager;
private Sensor mAccelerometer;
private ShakeListener mShakeDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);

........

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
            .setTitle("Alarm Clock")
            .setMessage("What to do with " +  alarmName + ": " + alarmTime + " " + alarmAMPM +"?")
            .setCancelable(false)
            .setPositiveButton("Dismiss", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                      alarmManager.cancel(pendingIntent);
                      i.putExtra("Status","Alarm Off");
                      i.putExtra("calledBefore",true);
                      i.putExtra("Alarm ID", alarmID);
                      i.putExtra("Alarm Name", alarmName);
                      i.putExtra("Alarm Time", alarmTime);
                      i.putExtra("Alarm AMPM", alarmAMPM);
                      i.putExtra("Alarm Sound", alarmSound);
                      i.putExtra("Snooze Type", snoozingType);
                      sendBroadcast(i);
                      dialog.dismiss();
                      finish();
                    //startActivity(startSetAlarm);
                }
            })
            .setNegativeButton("Snooze", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int id)
                {
                    alarmManager.cancel(pendingIntent);
                    i.putExtra("Status","Alarm Off");
                    i.putExtra("calledBefore",true);
                    i.putExtra("Alarm ID", alarmID);
                    i.putExtra("Alarm Name", alarmName);
                    i.putExtra("Alarm Time", alarmTime);
                    i.putExtra("Alarm AMPM", alarmAMPM);
                    i.putExtra("Alarm Sound", alarmSound);
                    i.putExtra("Snooze Type", snoozingType);
                    sendBroadcast(i);
                    Snooze();
                    dialog.dismiss();
                    finish();
                }
            });
    final AlertDialog alert = builder.create();
    alert.show();

And then I call the onDismiss method which I showed earlier. I also tried making a constructor of DialogActivity and initializing dialogactivity's object in main, to pass main's context into it however I get the error that "Dialog activity doesn't have a constructor with no parameters" If I make a default constructor, I still get an exception and it doesn't work.

UPDATE I figured it out. I had to retrieve my list stored in shared preferences in order to get the updated one. The updated list was only getting called in onCreate not in onResume. However I have another problem now. Which is still related to the same activity life cycle dilemma. If my application is closed (and is not in opened apps list) and I receive the dialog notification, If I dismiss the alarm, it turns off and the dialog goes away. However I can see my app now in the recent list. And when I open my app from there, my dialog activity is active and it just keeps on showing the dialog and doesn't let me enter the app. Unless I go to the app drawer and open the app from there. Here is a screenshot of what I mean. Dialog Problem

You must use BroadcastReceiver or EventBus library . Your activity before create not to be refresh you ListView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM