1

[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

user3156301
  • 45
  • 1
  • 9

3 Answers3

2

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"/>
VenomVendor
  • 15,064
  • 13
  • 65
  • 96
  • I tried to override backPress, but it doesn't finish the activity, it moves it to the background. This is my pending intent in the onRecieve method of broadcast reciever: PendingIntent.getActivity(context, 0, new Intent(context, B.class), 0); – user3156301 Jan 03 '14 at 13:23
  • It will finish the activity for sure, but the prob is that when you relaunch from recent apps, to avoid that you have to add `noHistory=true` to manifest – VenomVendor Jan 03 '14 at 13:41
  • Even i feel, it should. But it didn't. – user3156301 Jan 03 '14 at 13:50
  • Actually, to add to above, it does finish the activity, if the application is running. If the application is not running, the notfication starts the activity and on backpress, it doesn't finish it. – user3156301 Jan 03 '14 at 15:18
0

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.

Sandeep Tiwari
  • 2,042
  • 3
  • 24
  • 47
-1
@Override
public void onBackPressed() {
        Intent returnIntent = new Intent(B.this,A.class);
        B.this.finish();
        startActivity(returnIntent);

}

Try like this

Tamilselvan Kalimuthu
  • 1,534
  • 1
  • 13
  • 28
  • this might work, but the problem is, if user is already in the application and has gone to activity C (i.e. from A -> C), then on click of notification, it would open activity B. Now back press on B would start activity A, rather than bringing the user back to activity C. – user3156301 Jan 03 '14 at 13:29
  • you didnt mention anything about activity, the scenerio u told is different than this scenerio.. – Tamilselvan Kalimuthu Jan 04 '14 at 12:40
  • apologies but i thought it would be given. – user3156301 Jan 05 '14 at 03:08