invalidateOptionsMenu() not working
Clash Royale CLAN TAG#URR8PPP
invalidateOptionsMenu() not working
i want to update notification count badge on my android apps every time new notification receive from one signal notification class. the flow is, anytime new notification receive, i will save the count to sharedpreference. its already done and everytime notification receive, the count badge will be updated, problem is to update the icon in toolbar is using invalidate option menu.
So i make this method in my MainActivity class
public void updateNotif()
HashMap<String, Integer> notif = notif_count.getCount();
count = notif.get(NotifCountSession.KEY_COUNT);
Log.e(MainActivity.class.getSimpleName(), "COUNT : "+count);
invalidateOptionsMenu();
and then i call it inside OneSignal notification class like below
public class Notification implements OneSignal.NotificationReceivedHandler
private NotifCountSession session;
private Application app;
private int count;
public Notification(Application app)
this.app = app;
@Override
public void notificationReceived(OSNotification notification)
init();
count++;
session.saveCount(count);
//this code works
Log.e(MainActivity.class.getSimpleName(), "COUNT NOTIF : "+count);
//but this one not
new MainActivity().updateNotif();
private void init()
session = new NotifCountSession(app);
HashMap<String, Integer> notif = session.getCount();
count = notif.get(session.KEY_COUNT);
below is my notification count badge
how to solve this problem ?
new MainActivity().updateNotif()
so how to make it work ?
– Prasetyo
Aug 13 at 8:16
I suggest you should read about
Activity
first . Its the backbone of android . To make it work you can broadcast a message or call a static method .– ADM
Aug 13 at 8:17
Activity
1 Answer
1
Follow these step to update from notification.
1) Add in your MainActivity
private BroadcastReceiver mMyBroadcastReceiver;
Then ,
2) In onResume
@Override
protected void onResume()
super.onResume();
mMyBroadcastReceiver = new BroadcastReceiver()
@Override
public void onReceive(Context context, Intent intent)
updateNotif();
;
try
LocalBroadcastManager.getInstance(this).registerReceiver(mMyBroadcastReceiver, new IntentFilter("your_action"));
catch (Exception e)
e.printStackTrace();
// and your other code
3) Then unregister in onPause
@Override
protected void onPause()
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMyBroadcastReceiver);
4) Finally in your notificationReceived
method
notificationReceived
Intent intent = new Intent("your_action");
LocalBroadcastManager localBroadcastManager =LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(intent);
thank you very much :) u save my life
– Prasetyo
Aug 13 at 8:56
Welcome bro. Best of luck.
– Anisuzzaman Babla
Aug 13 at 9:45
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This line
new MainActivity().updateNotif()
Is a Disaster . Never ever create an Activity's instance . Its a component which instantiated by system itself .– ADM
Aug 13 at 8:12