Android: Make Your Notification even Sticky-er
1 min read

Android: Make Your Notification even Sticky-er

Android: Make Your Notification even Sticky-er

In my previous post, I've expanded things to make a notification sticky. This worked in my tests until yesterday, when I've found a new case where my notification would go away:

  1. Build an sticky notification as per the two previous posts
  2. Activate the notification - this will trigger the activity to be displayed on screen. The notification will remain shown
  3. Activate the notification again - this will make the notification go away, because the activity is already on screen

I've tried triggering the notification in onCreate() , onStart(), onPostCreate(), onResume() without any effect.

However, with the help of this answered question, I've managed to re-trigger the notification. The only interesting method that gets called when the activity is already on top is onNewIntent(). My activity code looks like this now:

public class MainActivity extends Activity {
    // ...

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        // debug
        Log.d(TAG, "onNewIntent() launched.");

        Handler mHandler = new Handler();
        mHandler.post(new DisplayStickyNotification(this));
    }

    // ...
}

You can probably trigger the notification directly via run() , but the most common method I've seen online is via a Handler.post() call.