-3

I am doing one application here I have one exit button when I click button,that time I need to exit from application..I tried using below code app is closing but it still running in taslmanager..but when I click exit button I should close app as well as I should remove from task manager how it's possible.

public class MainMenu extends Activity {
    Button exit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);

        exit=(Button)findViewById(R.id.btn1);
        exit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_HOME);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      startActivity(intent);
            }
        });
   }
 }
Cœur
  • 37,241
  • 25
  • 195
  • 267
user2401554
  • 121
  • 4
  • 19
  • possible duplicate of [Closing Application and killing it from ram](http://stackoverflow.com/questions/18229797/closing-application-and-killing-it-from-ram) – t0mm13b Aug 17 '13 at 12:13
  • also, [this](http://stackoverflow.com/questions/18238276/android-simulate-the-back-button-twice#comment26739948_18238276) – t0mm13b Aug 17 '13 at 12:14
  • 2
    http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238#2034238 – CommonsWare Aug 17 '13 at 12:14

4 Answers4

0

Use this flag:

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS.

This flag will notify the OS to remove this app/activity from the cache of recent apps.

intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

You can also add

android:noHistory="true"
android:excludeFromRecents="true"

in your AndroidManifest file for the activity where you don't want to see in recent apps.

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • tahnkyou..i gave that falg also but still app is there in taskmanager – user2401554 Aug 17 '13 at 12:15
  • you should use this flag with the `cleartop` flag you have in your code. Don't remove that flag either – Umer Farooq Aug 17 '13 at 12:20
  • Intent intent = new Intent( Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); startActivity(intent); – user2401554 Aug 17 '13 at 12:26
  • i gave like above in my exit onclik.but still app is there in taskmanager – user2401554 Aug 17 '13 at 12:26
  • This just removes the app from the recent list, not removing it from the OS cached apps. – Kai Aug 17 '13 at 13:04
  • Actually what do you mean by "remove application from task manager"? If `onDestroy()` of the activity is called , the app is closed. – Nizam Aug 17 '13 at 14:50
  • `onDestroy` is not guaranteed to be called immediately. – t0mm13b Aug 17 '13 at 15:34
  • after click the exit button if open taskmanager means my application name should not show in inside taskmanger list – user2401554 Aug 17 '13 at 15:47
  • If you followed the steps I showed, there is no way the app is still showing in your recent app. The above method worked for other people, why isn't it working for you? – Umer Farooq Aug 17 '13 at 15:52
  • hi..thease two lines i have keep for every activity..or else only first activity – user2401554 Aug 19 '13 at 03:33
  • Add those two line in the `` tag in Android Manifest file. – Umer Farooq Aug 19 '13 at 05:47
0

try this to exit the application

Intent startMain = new Intent(Intent.ACTION_MAIN); 

startMain.addCategory(Intent.CATEGORY_HOME); 
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(startMain); 
finish(); 
Aravin
  • 4,126
  • 1
  • 23
  • 39
0

Please think really hard about if you do need to kill the application: why not let the OS figure out where and when to free the resources?

Otherwise, if you're absolutely really sure, use

finish(); As a reaction to @dave appleton's comment: First thing read the big question/answer combo @gabriel posted: Quitting an application - is that frowned upon?

  • Now assuming we have that, the question here still has an answer, being that the code you need if you are doing anything with quitting is finish(). Obviously you can have more than one activity etc etc, but that's not the point. Lets run by some of the use-cases

  • You want to let the user quit everything because of memory usage and "not running in the background? Doubtfull. Let the user stop certain activities in the background, but let the OS kill any unneeded
    recourses. You want a user to not go to the previous activity of your app? Well, either configure it so it doesn't, or you need an extra
    option. If most of the time the back=previous-activity works,
    wouldn't the user just press home if he/she wants to do something
    else? If you need some sort of reset, you can find out if/how/etc
    your application was quit, and if your activity gets focus again you can take action on that, showing a fresh screen instead of restarting where you were. So in the end, ofcourse, finish() doesn't kill
    everthing, but it is still the tool you need I think. If there is a
    usecase for "kill all activities", I haven't found it yet

Sanket Shah
  • 4,352
  • 3
  • 21
  • 41
  • yes i have 2 activities..in 2 activities i have exit button if i click exit button in 2 activity means i have to come first activity i click exit btn in first activity menas i need close whole application – user2401554 Aug 17 '13 at 12:19
  • if you want to quit your one activity then just write First_activity_context_finish(); and moveTaskToBack(true); @user2401554 – Sanket Shah Aug 17 '13 at 12:23
  • noo..when i click first class exit button i need to close apllication.and remove application from task manager also – user2401554 Aug 17 '13 at 12:29
0

Android doesn't allow you to directly exit from the app.you just have to remove the your current activty from the stack before going to any new activity.

class first extends activity{ oncreate(){

Intent i = new Intent(getappcontext(),nextactivity);

stratactivity(i);

first.this.finish();

}

}

Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59