-1

I have a notification that started in a service. When user click on this notification, an activity starts.

My problem is: if user press back button in this activity, navigates to home screen instead of previous activity.

I want push new activity to existing task.

    Intent openIntent = new Intent(this, MediaPlayerActivity.class);
    openIntent.setAction(ACTION.OPEN_ACTION);
    PendingIntent openPendingIntent = PendingIntent.getActivity(this, 0, openIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, "")
            .setContentTitle(bundle.getString("Title"))
            .setContentText(bundle.getString("Description"))
            .setContentIntent(openPendingIntent)
            .setShowWhen(false)
            .setOngoing(true)
            .build();

    startForeground(NOTIFICATION.PLAYER, notification);

Sample 1:

Activity A -> Activity B -> (Notification click)

i want this:

Activity A -> Activity B -> Activity Z

OR

Sample 2:

Activity A -> Activity B -> Activity C -> (Notification click)

I want this:

Activity A -> Activity B -> Activity C -> Activity Z

OR

Sample 3:

Activity A -> Activity B -> (Home button) -> (Notification click)

I want this:

Activity A -> Activity B -> Activity Z

but I have this at all samples:

Activity Z ->(Back press)-> Home screen

H.Danesh
  • 53
  • 5

2 Answers2

0

are you override onBackPress method in that activity?

for this you most define onBackPress method in that activity like below to go to MainActivity when user press back button

@Override
public void onBackPressed() {
    finish();
    Intent intent=new Intent(thatActivity.this,MainActivity.class);
    startActivity(intent);
}
hamed
  • 148
  • 11
  • Previous activity is not always MainActivity. – H.Danesh Jul 21 '18 at 11:16
  • Sure. This was just an example @H.Danesh – hamed Jul 21 '18 at 11:22
  • This is not necessary or useful. Standard behaviour should take the user back to the previous `Activity` in his task. Your suggestion implies that the user knows what the previous `Activity` is. This is bad architecture and not necessary. – David Wasser Jul 25 '18 at 08:26
0

Finally i found the solution here.

by adding this line:

openIntent.addCategory(Intent.CATEGORY_LAUNCHER);
H.Danesh
  • 53
  • 5
  • This makes absolutely no sense. The linked question shows how to bring an existing task to the foreground **without launching a new `Activity` into it**. You want to launch a new `Activity` into an existing task. Please post your manifest and maybe we can figure out your problem. Adding a CATEGORY to the `Intent` shouldn't make any difference at all. – David Wasser Jul 25 '18 at 08:29