2

I have a Fragment F. After getting result from a service, F needs to finish the activity it is in and launch a page in the web browser which I do using an intent.

It does seem to work fine if the user presses the back button. However, if he launches the app from recent apps, the activity isn't finished. I have thought about otherways of doing it. Like finishing the current activity and opening the page from the parent activity. But I'll have to make a lot of changes in the flow. So that would be my last option. Is there any way to make sure that the activity is finished even when I launch it from recent apps?

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
getActivity().finish();

Edit: Added code.

2 Answers2

6

try this code for start browser and clear all the stacks of your application

Intent intent  = new Intent(); // need to set your Intent View here
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
getActivity().startActivity(intent);
getActivity().finish();

Updated

Try this attribute in your activity in AndroidManifiest.xml file

<activity android:name=".MainActivity"
        android:excludeFromRecents="true" ...
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • Don't forget that he is in a Fragment.. so it should be getActivity().finish() and getActivity.startActivity(intent) – Cata Jan 15 '13 at 06:51
  • I use Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url)); to open it in the default browser. Adding the flags doesnt make any difference. It still happens when **opening from recent apps**. When the user presses back, it works fine. –  Jan 15 '13 at 07:12
  • @Pratik: I initially thought about it. It avoids the problem. But felt the user will expect the app to be in the recent list. Wouldn't be fair to prevent the user from solving it because we have a problem right? –  Jan 15 '13 at 07:18
  • I guess choosing exclude from recents was the only option. The activity was ending as it was supposed to. Just that when launching from recents, the OS launched the app with the last activity. –  Jan 23 '13 at 04:17
1

use the following code

Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

this.finish();
Pratik
  • 30,639
  • 18
  • 84
  • 159
Yogesh Tatwal
  • 2,722
  • 20
  • 43