0

My app consists of just one activity (launchMode=singleTask) with a viewpager in it. In the app, the user schedules alarms through the AlarmManager. When these alarms are triggered, a broadcast receiver creates notifications in the status bar (wether the app is open or not).

I'd like to update a RecyclerView in the activity whenever a notification is issued AND THE ACTIVITY IS IN THE FOREGROUND. If the activity is in the background or not 'active' at all (doesn't exist), there's no need to call it to the front or open it just to update the RecyclerView.

How can I check if my activity exists before calling, say, "MyActivity.updateMyRecyclerView()"? (I have an idea on how to check if it's in the foreground or not, I think that wouldn't be a problem.)

bernardo.g
  • 826
  • 1
  • 12
  • 27
  • Can you just register your activity as a broadcast receiver itself and send your own broadcast (custom type) when your alarm is triggered? Then the activity could update its own recycler view. – Aleks G Sep 22 '17 at 23:38
  • which 1 do you want? check specific foreground-ness of activity or check foreground-ness of application? If you just want to call MyActivity.updateMyRecyclerView(), you can create broadcast receiver and put your "updateMyRecyclerView" code to that broadcast, then register the broadcast receiver to LocalBroadcastReceiver. I use this way to update recycler view from Firebase.onMessageReceived service – zihadrizkyef Sep 23 '17 at 00:15

1 Answers1

0

You can use broadcast receiver for your activity. You can send a broadcast and if your activity is 'alive' and is registered to receive the broadcast you can trigger the recyclerView update from there.

Send the broadcast when you are creating the notification like this

Intent intent = new Intent("key_to_identify_the_broadcast");
Bundle bundle = new Bundle();
bundle.putBoolean("updateRecyclerView",true);
intent.putExtra("bundle_key_for_intent", bundle);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

and in your Activity where you want to receive this intent, you can use a broadcast receiver

  private final BroadcastReceiver mHandleMessageReceiver = new 
  BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
    Bundle bundle = 
        intent.getExtras().getBundle("bundle_key_for_intent");
        if(bundle!=null){
            boolean shouldRefresh = bundle.getBoolean("updateRecyclerView");
            if(shouldRefresh){
               //Refresh your recyclerView
            }
        }

 }
};

You need to register and unregister the receiver to work

In your onResume method you can register this receiver to receive the broadcast

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    IntentFilter filter = new IntentFilter("key_to_identify_the_broadcast");
    LocalBroadcastManager.getInstance(this)
    .registerReceiver(mHandleMessageReceiver,filter);
}

You also need to unregister it before your activity gets paused

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    try {
     LocalBroadcastManager.getInstance(this)
         .unregisterReceiver(mHandleMessageReceiver);

  } catch (Exception e) {
    Log.e("UnRegister Error", "> " + e.getMessage());
  }
}
Shriyansh Gautam
  • 1,084
  • 1
  • 7
  • 13
  • Just to note, for something like this it's better to use LocalBroadcastManager: https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html – Daniel Nugent Sep 23 '17 at 00:31