Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Alex Feinberg
Alex Feinberg
2,025 Points

Ribbit enhancement help - how to refresh inbox on receipt of push notification

If I already have my Ribbit inbox open and I receive a push notification of a new message, I would like the inbox to automatically refresh its contents to show the new message. Of course, the swipe refresh layout is easy to use, but I'd still like to eliminate the extra step, even if it is just to learn how to do it.

In InboxFragment we already have a retrieveMessages method, so it should just be a matter calling that method from MainActivity when the notification is acknowledged, but how can MainActivity listen for a user responding to a notification so that it can react?

I added a Refresh option to my action bar menu just to experiment with calling the InboxFragment retrieveMessages method from MainActivity, but I haven't gotten that to work. Here's the switch case in MainActivity's onOptionsItemSelected method:

case R.id.action_refresh:
    if (mSectionsPagerAdapter!=null) {
        ((InboxFragment)mSectionsPagerAdapter.getItem(0)).retrieveMessages();
    }
    break;

This code dies with a null pointer exception because getItem(0) returns null, rather than the InboxFragment that I am expecting.

BTW... I think it would also make sense to automatically switch to the inbox tab if the user happens to be on the friends tab. It might even make sense to automatically refresh the inbox any time the user switches from the friends tab to the inbox.

Thanks, Alex

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Just discussed this in another post. I haven't tried any solutions myself, but check that other post for one recommendation of something to research and try.