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?
You can call System. exit(); to get out of all the acivities.
If the user wants to close down your app, they should swipe it off (close it) from the Overview menu.
Close one app: Swipe up from the bottom, hold, then let go. Swipe up on the app. Close all apps: Swipe up from the bottom, hold, then let go. Swipe from left to right.
<?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); } }); } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With