0

I wrote a Notification function to send notificaiton, and it will switch to a defined activity when user click the notification on the top bar. My code did what I want. But there is a problem.

Let's say there are 3 activities (A, B, C) and it away switch to A when user clicks the notification on the top bar.

The flow is:
(1) User open A
(2) Click a button at A and switch to B
(3) Click the notification on the top bar
(4) Switch to A (It is fine)
(5) Again, click a button at A and switch to B
(6) Press Home Button to hide the App
(7) Long press the Home Button and click the App icon
(8) Switch to A (??? It should switch to B as I am on Activity B!)

---- How can I avoid it to switch to Activity A when I switch to my App by long pressing Home Button? Thanks!

Here is the code snippet:

int notificationCounter = 0;
private void displayNotificationMessage(String title, String message, int notification_id, String fromUser, boolean vibrate, boolean playSound, String notificationActionStr)
{

    if (receiverMessageBroadcast || !currentTargetUser.equals(fromUser))
    {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        //intent.setAction(notificationAction + "_" + System.currentTimeMillis());
        Bundle bundle = new Bundle(); 
        bundle.putInt("notificationAction", 1);
        bundle.putString("targetUser", fromUser);
        intent.putExtras(bundle);



        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Notification notification = new Notification(R.drawable.notification, message, System.currentTimeMillis());
        notification.setLatestEventInfo(this, title, message, PendingIntent.getActivity(this.getBaseContext(), notificationCounter, intent, PendingIntent.FLAG_UPDATE_CURRENT));


        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;     


        if (playSound)
            notification.defaults |= Notification.DEFAULT_SOUND;


        if (vibrate)
            notification.vibrate=new long[] {100L, 100L, 200L, 500L};


        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;


        notificationMgr.notify(fromUser, 0, notification);
        notificationCounter++;          
    }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
mobile app Beginner
  • 1,651
  • 3
  • 26
  • 40
  • are you closing your app on home press? – drulabs Apr 19 '12 at 16:46
  • This might help if all else fails http://stackoverflow.com/questions/9645159/hide-android-application-in-long-press-home-button-menu – Luke Apr 19 '12 at 16:48
  • Thanks KKD, I expect the user press the Home Button to hide the App. They can open the App again with latest Activity when they long press the Home Button. My problem is it always shows the Activity defined by the notification (Which is wrong, it should show the latest Activity). – mobile app Beginner Apr 20 '12 at 04:23
  • Thanks Luke, but the solution does not work in my case – mobile app Beginner Apr 20 '12 at 04:23

1 Answers1

0

This is the distinction between “activities” and “tasks”, as explained here. Basically, it is tasks that show up in the back stack and that the user can return to, not activities.

You could try marking activity B with “android:exported="true"” in your manifest.

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13
  • Thanks Lawrence, but it cannot solve the problem. A and B are in the same application. I switched to Activity at (5). Therefore, it should resume B at (8). Is it something wrong with my displayNotificationMessage() method? It always resumes B after the first time that it resumes the application by clicking the notification at the status bar. I have struggled for few days, but still cannot figure what's wrong. Thanks for your hints – mobile app Beginner Apr 22 '12 at 13:35