12

How to kill whole application in onne single click.. finish() is not working?? it redirects to tha previous activity...pls guide me.

          public void onClick(View arg0) {
            // TODO Auto-generated method stub
            WallpaperManager wp = WallpaperManager
                    .getInstance(getApplicationContext());

            try {
                Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                        .getDefaultDisplay();
                int width = d.getWidth();
                int height = d.getHeight();
                wp.setBitmap(b1);
            } catch (IOException e) {
                Log.e("error", "Cannot set image as wallpaper", e);
            }

                        finish();

        }
Coder
  • 186
  • 1
  • 1
  • 9
  • Although people are answering your question you should really look at Gunaseelan's post & read "Quitting an application - is that frowned upon?" http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon. – Ryhan May 31 '13 at 07:29
  • Sorry,i have tried...but not working – Coder May 31 '13 at 07:40

5 Answers5

31

System.exit() does not kill your app if you have more than one activity on the stack

Use android.os.Process.killProcess(android.os.Process.myPid()); this way.

for sample

public void onBackPressed() {
    android.os.Process.killProcess(android.os.Process.myPid());
    super.onBackPressed();
}

For more detail see Is quitting an application frowned upon? , How to force stop my android application programmatically?

I hope this will help you.

Community
  • 1
  • 1
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • i have two activity.activity one display iamges in gridview and second activity display selected image in fullview.when i click on set wallpaper both activity should b closed na...but it didn;t work by using aby finish,system,exit(0),process kill... – Coder May 31 '13 at 07:39
  • 1
    may be m wrong..but i write finish in both activity after working.. now its working fine :) – Coder May 31 '13 at 08:26
5

You can use this function to close your application

Process.killProcess( Process.myPid() ); 
Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
  • 2
    int pid = android.os.Process.myPid(); android.os.Process.killProcess(pid); i wrote this command..but it didn't work for me... it just kill the current activity – Coder May 31 '13 at 07:24
1
  • Use startActivityForResult to start any activity.
  • Write finish() in onActivityResult of the activity in which you are starting another activity if result == RESULT_CANCELLED.
  • Finally use finish() at the point of exit.
user1721904
  • 843
  • 4
  • 13
1

i think you want this

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
Senthil
  • 1,244
  • 10
  • 25
1

Finish ()

Added in API level 1 Call this when your activity is done and should be closed. The Activity Result is propagated back to whoever launched you via onActivityResult().

you must use System.exit() to kill your application on a click to directly close the activity.

Why finish() is not working for you ?

Take an example where Activity A had called Activity B and when you call finish() on your activity B if finish the activity but as Activity A had given him called and it is a previous activity it returns back you to Activity A screen

What you can do?

  1. You can call System.exit() - It will directly kill the application wherever it is called from it does not matter.
  2. Use Process.killProcess(your process id);

  3. Start the home activity by killing your current activity by using finish() and then call

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    
NetStarter
  • 3,189
  • 7
  • 37
  • 48