Thank you for taking the time to read my question. I have an application where my app has a navigation drawer along with the icons, title, and counter. I don't have enough reputation to add image sorry. Here's a link with an example.
I set the counter to actually be a broadcast receiver where it will show the updated count once a specific event is triggered. The flow that triggers the event is as follows:
- a
Fragment
is started. - once the fragment is started it calls a method which inserts this class name into a table called history (this table history will be used to retrieve the class name and create a new activity of that class).
- in the same fragment there is a method which sends a local broadcast event to the receiver which is simply the count of the history table. The count is done by a method which reads the DB and retrieves the count and stores the value into a string. It then sends the value to the receiver which will pick up the value using the
getExtraString()
method inLocalBroadcastManager
API. - once you click the back button you go back to the main screen.
- now you click on the slider menu icon to open up the navigation drawer and the count for the History tab is still 0.
Any reason to why it did not update based upon the received broadcast event?
Here is the code that adds the items to the navdrawer and the counter which excepts broadcast receiver.
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));//Replace 22 with method to select count of history
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1), true, mMessageReceiver));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
Now here is the code that registers the broadcast receiver.
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
Here is the receiver that stores the value sent by the local broadcast sender.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
String test = intent.getStringExtra(getHistoryCount());
//SendBroadcast send = new SendBroadcast();
//send.sendLocationBroadcast(intent);
}
};
Here is the getHistoryCount()
method which you see in the code above. This just reads the count for the HISTORY table.
private String getHistoryCount() {
String _count = "0";
FeedReaderDBHelper dbHelper = new FeedReaderDBHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor count = db.rawQuery("SELECT * From history", null);
_count = Integer.toString(count.getCount());
return _count;
}
Here is the fragment class which handles the insert into the db and the sending event to the local broadcast receiver.
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
insertIntoDB();
setListAdapter(new OsViewAdapter(getActivity(), vals));
sendMessage();
}
Here is the methods definition from the above method which sends the broadcast even to the local broadcast receiver. This is defined in the same class as the above code that has the onActivityCreated() method.
private void sendMessage() {
Intent intent = new Intent("custom-event-name");
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
}
private Context getContext() {
// TODO Auto-generated method stub
Context context;
context = getActivity();
return context;
}
This line expects some key to be set in intent extra as the history count while broad cast.
Try this:
and in broadcast onReceive: