0

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 ::

  1. i want my application to quit(close all activities and go to homescreen)
  2. But i don't want my apk to be removed from android system itself
  3. 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();

4 Answers4

0

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.

Blo
  • 11,903
  • 5
  • 45
  • 99
  • [+1] for your input ... please look at the edit ... i have some deadlock in solving this issue –  May 04 '14 at 09:29
  • I don't clearly understand @Sky when you said: "*the **previous** activity but it is not **canceled**, its just reloaded*". Can you explain a little more this please? – Blo May 04 '14 at 10:56
  • I am able to launch the Activity1(which is a splash screen) ..... but i dont know where to place the finish() code .... still the activity is not closing(splash(Activity1) is the first activity)..... i added bundle and tried as shown in this question ....please check it http://stackoverflow.com/questions/23455326/finish-not-working-in-closing-the-activity i think i need to add more conditions i guess –  May 04 '14 at 11:06
  • As I can understand on your other question @Sky, you should call `finish` after `startActivity` in *startingNewActivity* method. `finish` is normally calling when another activity is launched. – Blo May 04 '14 at 11:08
0

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

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

Do like this

Intent intent = new Intent(Activity1.this, Activity2.class);
startActivity(intent);  
Activity1.this.finish();
Meghna
  • 539
  • 4
  • 14
  • if you use this finish current activity only,if one more activity its not working – learner May 04 '14 at 05:43
  • Sure @Boopathi, but if you use `finish` for each activity, this resolve the issue. – Blo May 04 '14 at 05:45
  • @boopathi,ya you wont get that previous activity by pressing back key – Meghna May 04 '14 at 05:47
  • @Sania if use 3 or more activity its doesn't work.1.i want my application to quit(close all activities and go to homescreen) – learner May 04 '14 at 05:51
  • @Fllo everyone keep the all activities alive until logout.If you use every activity finish(),its doesn't give good application – learner May 04 '14 at 05:53
  • can u post your exact class(only finish part) where it is not working.because the logic is same for all activities,i think u r doing something wrong,as i have used it for 30 classes,it has no issue, – Meghna May 04 '14 at 05:54
  • 1
    Not necessary, @Boopathi, all depends on the app you built! However, I didn't use this feature (finish each activity when you lanch another) for the reason that you mentioned (kill activities with logout). – Blo May 04 '14 at 05:56
  • 1
    @Boopathi : **"everyone keep the all activities alive"** This is not true. There are very many cases where I finish an `Activity` after starting a new one. It depends entirely on the design of the app and what each `Activity` does. – Squonk May 04 '14 at 05:57
  • If you use 30 activity ,your code doesn't kill the all activity,they asked the question is when i go to home screen ,my previous activity have been killed. – learner May 04 '14 at 06:00
  • it is finishing your current Activity by finish(),so u are able to go to homescreen.Where you are getting problem.i think sky problem got solved by this method – Meghna May 04 '14 at 06:02
  • @Squonk for example you have 30 button in activity,30 buttons redirect to some other activity ,at that time you have given a finish() in all onclicklistner of buttons.Its doesn't give a code efficiency. – learner May 04 '14 at 06:05
  • after every intent i do like this,in case of 30 button,i can handle it by writing once(i ll prefer use switch case) – Meghna May 04 '14 at 06:09
  • @Boopathi : If you have 30 buttons in an `Activity` then you should have the `Activity` implement `View.OnClickListener` with a single `onClick(...)` method instead of using 30 anonymous `OnClickListener` instances. – Squonk May 04 '14 at 06:09
0
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);
learner
  • 3,092
  • 2
  • 21
  • 33