Finish Current Activity and start a new one - android

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.

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

use the following code
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();

Related

How to clear previously opened activity in Android

Intent intent = new Intent(MainActivity.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Opening coupon activity to apply coupon:
Intent intent = new Intent(Payment.this, Coupon.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
From coupon activity opening payment activity with updated data
Intent intent = new Intent(Coupon.this, Payment.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
The problem is, when I click on the back button on payment activity after applying the coupon, it opens the previously opened payment activity again (not the main activity).
onBackPressed() I don't want to do it static (like Intent intent = new Intent(Payment.this, MainActivity.class); and also don't want to use finish(); in payment activity.
Please help.
You can use FLAG_ACTIVITY_NO_HISTORY when launching the Payment activity.
See: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NO_HISTORY
This is a Simple Use case of startActivityForResult don't make it complicated with Intent Flags and launch modes.
Whenever you Open your Activity Coupon just use startActivityForResult and upon applying coupon send the result back to previous Activity (success/failure) or whatever result you want . Then you can change the UI as per the result you got in onActivityResult() .
You can follow How to manage startActivityForResult on Android or the latest way from the doc Getting a result from an activity .

Does the Intents get killed after i pass to another Intent Android

I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();

Closing an android application programatically in android

I have a application that has two activities
Activity1
Activity2
Now i start Activity1 and move to Activity2 now i want to close the application at a moment
If i use finish() i am able to close only Activity2 and not
Activity1
What i want to do ::
i want my application to quit(close all activities and go to
homescreen)
But i don't want my apk to be removed from android system itself
How can i achieve this ?
{Edit}
I used the code(Activity2 has a fragment which launched a dialog on
"ok" condition in positive condition i am performing this condition below)
I am able to launch the previous activity but it is not canceled, its
just reloaded
Intent intent = new Intent(getActivity(), SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
getActivity().finish();
You need to make your Activity2 as "top task" with a Flag when you call it as follows:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
See the Reference:
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished.
Then, call finish() in your Activity2. This should do the trick.
There is a way to kill your application process, but it is not recommended
this.finish();
Process.killProcess(Process.myPid());
This will kill your application and releases all the memory associated with it.
Note: Assuming your app has a single process id
Do like this
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);
Activity1.this.finish();
Intent intObj=new Intent(this, Homescree.class);
intObj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intObj);

Clearing all activities in android

I have 4 Activities 1.Home,2.B,3.C and 4.D. Whenever I start Home from Activity D I want to finish all other activities. I Used this code, but when I press back button from Home it brings me to the previous activity. What I did wrong here.?
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent)
You can try this,
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Note: As described in FLAG_ACTIVITY_CLEAR_TOP documentation
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will work only for activities which are still in the activity stack. I believe you are finishing the Home Activity when going to B. So that CLEARTOP won't work.
Now try something like this.
You need to set an Extra with intent Of "D" to Home. Then you have to check the Intent extra in Home, call finish() if the extra matching
Intent intent = new Intent(contxt, Home.class);
intent.putExtra("urString",defaultvalue);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// Checking the intent extra at "HOME"
if(getIntent().hasExtra("urString")){
// manage your own way
finish();
}
In manifest.xml file set android:nohistroy="true" for all the activities

Which intent flag do I need to use so that When When I press back I don't see that activity again?

I have an activity that leads to another. i want to make it so that the first activity never opens again once it directs to the other one
clear_top doesnt work. which flag is needed?
This will start a activity with new information and no history
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
MainActivity.startActivity(intent);
For your Reference, you can read android documentation : Click Here
You can also use android:noHistory="true" for the first activity in your AndroidManifest.xml. No need for intent flags.
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
i = new Intent(HomeActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
That should do it.
You could just call finish() in your first Activity after your call to startActivity(...).

Categories

Resources