user1979424
user1979424

Reputation:

Finish Current Activity and start a new one

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.

Upvotes: 2

Views: 9219

Answers (2)

Yogesh Tatwal
Yogesh Tatwal

Reputation: 2751

use the following code

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

startActivity(intent);

this.finish();

Upvotes: 1

Pratik
Pratik

Reputation: 30855

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" ...

Upvotes: 6

Related Questions