1

Asume I have two Activities:

  1. MainActivity: Shows a list of items that are stored in a SQLiteDB
  2. NotificationActivity: Activity from which the user can add an item to the database.

NotificationActivity is opened when a user clicks on the notification. Next, the user adds an item to the database and the NotificationActivity is closed. How can I inform my MainActivity that an object was added?

What won't work:

  • I can't use a broadcastreiceiver because it is unregisterd when the activity is paused, which ofcourse happend when my NotificationActivity opens.

  • I can't use startActivityForResult because NotificationActivity opens from a notification.

  • I can't use MainActivity its onResume because I don't to update the list every time the MainActivity is resumed.

So how can I send a message/object to my MainActivity from my NotificationActivity that was opened from a notification?

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116

2 Answers2

1

If what you are trying to achieve is update the UI in Main Activity with the new item added from the Notification Activity and you are using Cursor Loaders and Content Providers in your activities checkout the concept of Cursor.setnotificationuri() What is cursor.setNotificationUri() used for?

If you enable Cursor.setnotificationuri() in your query api in ContentProvider, any change during update, insert or delete in Content Provider is immediately reflected into the UI.

Community
  • 1
  • 1
takesavy
  • 135
  • 2
  • 16
1

Looking at the comment exchange between you and @Hulk, you have the following options:

  1. Use a BroadcastReceiver. You say that you need to unregister the receiver to prevent "Activity has leaked IntentReceiver". Yes, you should unregister your receiver, but you don't need to do it in onPause(). You can do it in onDestroy() when your Activity is destroyed. The BroadcastReceiver can stay active listening while your Activity is alive. There is no memory leak here.

  2. Use a static (global) variable, that is set to true when the NotificationActivity makes a change to the data. In MainActivity.onResume() you can check this variable and update the UI accordingly.

David Wasser
  • 93,459
  • 16
  • 209
  • 274