0

My problem is like that: When I'm pressing the notification i got, some activity is open, lets say activity number 3. But when I press the back button, I navigated by the android to the menu. What I want to do is to preserve the correct navigation experience of the user, and when I'm pressing the back button it will take me to activity number 2, and when I'm pressing there on the back button, it will take me to activity number 1, and so on.
Another problem I have is that, when I'm opening activity number 2, I need to set there some extra data in the intent, but I don't know how to set it.
Summery of my problem: preserve the correct navigation of the user and set some extras for the activity that will be open by the back button click.

Here is my notification code:

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("New Question")
                .setContentText("" + CurrentQuestion.getAuthor().getUserName() + ": " + CurrentQuestion.getQuestion() + "");

        Intent openHomePageActivity = new Intent("com.example.ihelp.HOMEPAGEACTIVITY");
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntent(openHomePageActivity);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, mBuilder.build());

Thanks!

Yossi Zloof
  • 1,609
  • 3
  • 13
  • 13
  • override onBackPressed() method and add code that will navigate to desired activity – Ajit Feb 27 '13 at 12:47
  • But that won't do problems? Because when I'm pressing the back button it is going to the last activity that had been stopped. But if I'm pressing the back button and override the onBackPressed() it will open me a new activity and not resume the last activity that had been stopped. And that not what I want that happen every time I'm pressing the back button.. – Yossi Zloof Feb 27 '13 at 13:00
  • I would try the next option: By pressing on notification you will open activity number 1, but then immediately go to number 2 and then immediately go to number 3. Then by clicking on 'back' button from activity number 3, he will go back to activity number 2. The only thing you need to care is that all the UI stuff won't be created in this case for activities number 1 & 2. I am pretty sure, that the user won't feel like he just was in activity 1 & 2 before opening number 3 because of the speed. – sromku Feb 27 '13 at 14:14
  • Thank you, Don't you think it is a crapy way to do this? If not I will go with it! – Yossi Zloof Feb 27 '13 at 17:46

3 Answers3

1

Looks like you want to synthesize a back stack: http://developer.android.com/training/implementing-navigation/temporal.html#SynthesizeBackStack (see "Create back stack when starting the activity")

and also http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse ("Preserving Navigation when Starting an Activity" as a "Regular activity")

qix
  • 7,228
  • 1
  • 55
  • 65
1

Anyone in same scenario can use this solution that I use in my apps. Override onBackPressed (in your activity routed from notification) and check if your app is in stack or not. if not then start your root activity. User can always navigate from your root activity. This is what I do in my apps

@Override
public void onBackPressed() {
    if (isTaskRoot()) {
        Intent intent = new Intent(this,YourMainActivity.class);
        startActivity(intent);
        super.onBackPressed();
    }else {
        super.onBackPressed();
    }
}
Mudassar
  • 1,566
  • 4
  • 21
  • 31
0

Save the parameters you need in onSaveInstanceState & get them back in onRestoreInstanceState. that way it won't matter if your activity was paused by any other notification as well.

Ashwini Bhangi
  • 291
  • 1
  • 6