[UPDATE BELOW]

I have application with multiple activities. I have issues in handling the activity started from the click of notification item.

Below is the app structure:

  1. For normal flow/working, user start the application and Activity A being the launch Activity, it starts. User could navigate to activity B from activity A (i.e. A -> B)
  2. On click of back button on B, activity A is displayed.

However, the issue is with starting activity from the click of notification. Below is the flow:

  1. User clicks on the notification
  2. Activity B is started
  3. On click of back button on activity B, it doesn't start activity A (which is understand) but it doesn't kill the activity B either (moves activity B to background). What I want is, if the activity B is started by the notification click, then back press on B should either takes user to Activity A or kill activity B.

Any pointers in this regard would be appreciated.

[UPDATE]:

Strange but, this is what is happening now. 1. Overridden the onBackPressed event in activity B. 2. In this method, finish() is called.

If the application is already running and on click of notification, activity B is displayed and then on back press, the activity B is finished. However, if the application is not running, notification click starts the activity B and on back press, it does call the backpress event and finish() method but the activity doesn't finish and goes in the background. This is really weird. Or may be I am missing some details :((

Thanks Shrey

有帮助吗?

解决方案

Override in onBackPressed().

@Override
public void onBackPressed()
{
     finish();
     super.onBackPressed(); 
}

But, user can directly got to activity B, if he/she start's your app from recent apps. To avoid this, add below to your ActivityB in Manifest. If you are navigating to ActivityC from here, pressing back in ActivityC will not return to ActivtyB so override onBackPressed() and relaunch ActivityB via Intent.

    <activity
        android:name="ActivityB"
        android:clearTaskOnLaunch="true"
        android:excludeFromRecents="true"
        android:finishOnTaskLaunch="true"
        android:noHistory="true"/>

其他提示

follow this flow:-

1- check isLauchFrom in oncreate() of Activity A.

2- If isLaunchFrom Notification then navigate on Activity B.using startActivityFor Result.

hope this will help you ,If any Confusion send comment.

@Override
public void onBackPressed() {
        Intent returnIntent = new Intent(B.this,A.class);
        B.this.finish();
        startActivity(returnIntent);

}

Try like this

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top