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!!!


6 comments:

Sami said...

Hi,
I have to submit the timetable app to my Professor as an assignment,
so please can any1 send me the code(java,xml,manifest etc) which
creates the timetable of an individual professor and displays the
timetable of the desired professor.
Main activity shud contain two Buttons(create, Display).
In create Activity, there shud be fields to add Professor name,
subject name, time, day, and add button to add the details & delete/
modify butoon to delete/modify the particular details.
In Display Activity, there shud be a spinner according to the
professor name we created, and after selecting or clicking on that
particular professor item, it shud show the timetable of that
particular professor.
I have tried it, but I am getting a lot of errors.

So please help me out...
Thanks in advance
Sam.

AndroIndu said...

Sami,
what is the error are you getting?

mr.haps said...

HI.

What should I do, if I want that when I click Back Button in Second Activity, it takes me directly to Root Activity.(First Activity flushes out from the Stack)
I have also used a Submit button in First Activity which shows a PopupWindow, which asks to confirm action and onClick to OK Button we move to Second Activity.

Thanks,
Harpreet.

AndroIndu said...

Harpreet,
you can call finish() in ok button event.then start second activity.
so that after cliking on ok button firstactivity will destroyed and second activity will start.In this if you are pressing back button in second activity it will directly go to root activity.

RootActivity --->FirstActivity(call finish() in ok button)--->secondActivity.

mr.haps said...

Thanks AndroIndu.

It worked well. I thought that I have to add and implement some sort of methods, but it was so simple.


Do you have any example for progress bar?

On clicking OK it takes some time to fetch the form from server, so I was thinking to show the progress bar at that location.

Thanks,
Harpreet.

Unknown said...

When their are activities having asynctask,then this method not working.