Thursday, 2 February 2012

How to Exit from android Application

Frankly Speaking, till date I have never seen exit functionality in Android Application.
While browsing through an application sometimes we need to exit from an particular application.
For this purpose, find a post below which explains implementation of an exit functionality from an application.
 Prior to this, I want to explain Launcher mode “SINGLE TASK” , with the help of this mode we can exit from any screen of an application. 
Single Task :- If the launcher mode is single task then Activity start from this will always be on the top of stack.If we set launcher mode of Root Activity of application is SingleTask then whenever we relaunch this root Activity then all Activity on Top of this Root Activity will be destroyed.

Step 1: create Root activity as below code.
        set android:launchMode="singleTask"  in Manifest file Launch FirstActivity on Click event of next button.Please note restart call back function.I have called finish() here.So that whenever RootActivity will relaunch ,it will exit from whole application.   

public class RootActivity extends Activity {
    Button next = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(RootActivity.this,
                        FirstActivity.class);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
                finish();
    }
}
Step 2:-Create First Activity as below,Start Second Activity on next button event
public class FirstActivity extends Activity {
    Button next = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(FirstActivity.this,
                        SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}



Step3:-Create second Activity as below.Implement Exit button code here.I have relaunch RootActivity on exit button event.Thus all the activity on the top of RootActivity will finish since I set Launchor mode of RootActivity is SingleTask.
public class SecondActivity extends Activity {
    Button exit = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        exit = (Button) findViewById(R.id.Button01);
        exit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(SecondActivity.this,
                        RootActivity.class);
                startActivity(intent);
            }
        });
    }
}
Step 4:-Create manifest file as below,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.androindu.com"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".RootActivity"
                  android:label="@string/app_name" 
                  android:launchMode="singleTask"               
                 >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".FirstActivity"
        />
        <activity android:name=".SecondActivity"
        />
    </application>
</manifest>

Step 5:-Create required xml layout as below,
first.xml

<?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"
    android:gravity="center"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="First Activity"   
    />
<Button
    android:text="Next"
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>

==================================================================================
second.xml

<?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"
    android:gravity="center"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Root Activity"   
    />
<Button
    android:text="Next"
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>

=====================================================================================
main.xml

<?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"
    android:gravity="center"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Root Activity"   
    />
<Button
    android:text="Next"
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</Button>
</LinearLayout>

That's It.
Happy Androing!!!