Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Application with Exit button [duplicate]

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?

like image 236
Thinkerbelle Avatar asked May 16 '11 06:05

Thinkerbelle


People also ask

How do you exit an application?

You can call System. exit(); to get out of all the acivities.

How do I close an app from pressing the back button?

If the user wants to close down your app, they should swipe it off (close it) from the Overview menu.

How do I close an entire app on Android?

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.


2 Answers

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);         }     });     }  } 

like image 92
Nikhil Avatar answered Oct 04 '22 19:10

Nikhil


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.

like image 30
Sparky Avatar answered Oct 04 '22 21:10

Sparky