50

I'm a beginner in android, I'm practicing a Project that have a 2 labels and 1 exit button. But when I run this project in android phone the exit button is not working, it won't exit at all.

How can I make exit button work?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Thinkerbelle
  • 749
  • 3
  • 9
  • 17

6 Answers6

82

Below used main.xml file

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/txt1" android:text="txt1" />
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/txt2"   android:text="txt2"/>
<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/btn1"
    android:text="Close App" />
  </LinearLayout>

and text.java file is below


import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class testprj extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            System.exit(0);
        }
    });
    }
 }

Nikhil
  • 16,194
  • 20
  • 64
  • 81
46

Don't ever put an Exit button on an Android app. Let the OS decide when to kill your Activity. Learn about the Android Activity lifecycle and implement any necessary callbacks.

Sparky
  • 8,437
  • 1
  • 29
  • 41
  • 4
    The only proper answer out of 4 responses here, imho. – EboMike May 16 '11 at 07:08
  • 6 answers now, this is still the only right one (well, plus the one with the atrocious spelling). Scary to see how many people think terminating an app is a good idea. – EboMike May 16 '11 at 07:20
  • in a perfect world it's a bad idea. if you are not a perfect programmer (which is hard, esepcially in a far from perfect operating system) it can sometimes be good to exit the application so that users dont get stuck in a frozen state (some users might not know how to kill the application themselves). i have written an app for a client that basically wanted to restart the app everytime the user comes back to it. i decided to put an exit(0) function in to start fresh every run. – Joris Weimar Jun 18 '12 at 18:51
  • 1
    as an example (although this is not android): i have had numerous problems with the netflix app on my ipad. i would often get stuck in a frozen state. i had to manually exit the app to get back in. i'm not saying that "exit(0)" fixes this, but it certainly creates a better user experience in times of trouble (especially for novice users). the programmer could still be made aware of the issue by means of exception handling, etc – Joris Weimar Jun 18 '12 at 18:56
  • Check out this video from Android Developers which explains this in detail. http://www.youtube.com/watch?v=631T7B8HOv4 – Tim Kist Feb 06 '13 at 12:23
  • 9
    @EboMike I disagree. Apps having background services that clog network, unessecary notifications, brodcasts which drains battery and can cause worse user experience(clogging of notification area). Yes Android does take care of resources when needed, this however is not good either as Android must take the time to do so and can cause the OS to blink/slightly lag depending on the app, but when the user KNOWS he/she wants to 100% leave the app with nothing running due to battery drainage etc. he/she should be able to. Also stops the chance of a memory leak from poor developers. – basickarl Nov 04 '13 at 16:09
  • 4
    There are some kind of apps that HAS TO be closed by user. The apps involve security, collect secure data and so on... – TomeeNS Sep 27 '15 at 13:31
  • 2
    I think an exit does have some use; I am making a stealth app that aborts the whole operation after 5 flings and I want it to exit the whole thing, not just that activity. It'd be awesome if it could lock the phone, too. – Script Kitty Nov 29 '15 at 22:57
  • Hope it was a comment! – Gourav Mar 04 '19 at 16:44
12

i try this

Button btnexit = (Button)findviewbyId(btn_exit);

btnexit.setOnClicklistenr(new onClicklister(){

     @override
     public void onClick(View v){
            finish();
});
EboMike
  • 76,846
  • 14
  • 164
  • 167
Hitesh Dhamshaniya
  • 2,886
  • 3
  • 24
  • 31
  • 1
    but when BACK BUTTON pressed, its moves to earlier screens.how to handle that like [session flush] – Priyan RockZ Nov 14 '13 at 08:12
  • @hitesh this code is only for back activity how it work ......no nope this will not work if we are in second activity and third activity then this will fail to exit . it will work only back ......if we make this in splesh or first activity then we can exit only ...... – Amitsharma Feb 28 '14 at 08:21
12

try this for close app

Activity.finish();
System.exit(0);
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • if we are in second,third activity then can we exit directley at this app....i think this is only for back .......if we do this in main activity or first activity then we can exit only ......other then this can not work for exit this will only back....... – Amitsharma Feb 28 '14 at 08:24
  • 2
    works for only close the activity and does not work to close the app in the second activity! – Hamid Aug 04 '16 at 08:06
11
this.close_Button = (Button)this.findViewById(R.id.close);
   this.close_Button.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
        finish();
     }
  });

finish() - Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

CMA
  • 2,758
  • 5
  • 28
  • 40
  • 2
    works for only close the activity and does not work to close the app in the second activity! – Hamid Aug 04 '16 at 08:07
6

You cannot exit your application. Using android.finish() won't exit the application, it just kills the activity. It's used when we don't want to see the previous activity on back button click. The application automatically exits when you switch off the device. The Android architecture does not support exiting the app. If you want, you can forcefully exit the app, but that's not considered good practice.

Pål GD
  • 1,021
  • 8
  • 25
star angel
  • 520
  • 2
  • 7
  • 14