Android completely quits application implementation code _android

Source: Internet
Author: User
Tags stub

The Android Exit application calls Android.os.Process.killProcess (Android.os.Process.myPid ()) or system.exit (0), which is only for the first activity ( Which is the entry activity). If you have a a,b,c of three activity, and you want to quit in B or C, calling the above method, you will often destroy the current activity and return to the previous activity. Of course, you can return to the previous activity one by one, until you jump to the entry's activity and finally exit the application. But this is more cumbersome, and the experience of returning one after the other is not friendly.

The popular approach on the web is to define stacks, write a exitapplication class, manage the activity using a single case pattern, and invoke exitapplication.getinstance () in each oncreate () method of the activity. Addactivity (This) method, which calls the Exitapplication.getinstance (). Exit () method when exiting to completely exit the application.
Exitapplication class

The code is as follows:

Copy Code code as follows:

Import java.util.LinkedList;
Import java.util.List;

Import android.app.Activity;
Import android.app.Application;

public class Exitapplication extends application {

Private List activitylist = new LinkedList ();
private static exitapplication instance;

Private Exitapplication ()
{
}
To get a unique exitapplication instance in a single case pattern
public static exitapplication getinstance ()
{
if (null = = instance)
{
Instance = new Exitapplication ();
}
return instance;

}
Add an activity to the container
public void addactivity (activity activity)
{
Activitylist.add (activity);
}
Traverse all activity and finish

public void exit ()
{

For (activity activity:activitylist)
{
Activity.finish ();
}

System.exit (0);

}
}

The following three classes of Indexactivity, Bactivity,cactivity is a simple example, respectively, is the indexactivity–>bactivity–>cactivity jump order. The Exitapplication.getinstance (). Addactivity (activity activity) method is called in the OnCreate () method in each activity class. When any of the activity interfaces exit the application, the application can be completely exited in any activity by invoking the Exitapplication.getinstance (). Exit () method.
Indexactivity Class Source code:

Copy Code code as follows:

Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Indexactivity extends activity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Button next= (button) Findviewbyid (R.id.next_to_b);
Next.setonclicklistener (Nextclick);

Button exit= (button) Findviewbyid (R.id.exit_main);
Exit.setonclicklistener (Exitclick);
Exitapplication.getinstance (). addactivity (this);

}

Onclicklistener nextclick=new Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub

Intent intent=new Intent (indexactivity.this,bactivity.class);
StartActivity (Intent);

}
};

Onclicklistener exitclick=new Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Exitapplication.getinstance (). exit ();
}
};
}

Bactivity Class Source code:

Copy Code code as follows:

Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Bactivity extends activity {

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);

Setcontentview (r.layout.b);
Button next_to_c= (button) Findviewbyid (R.id.next_to_c);
Next_to_c.setonclicklistener (Next_to_cclick);

Button exit_b= (button) Findviewbyid (r.id.exit_b);
Exit_b.setonclicklistener (Exitclick);
Exitapplication.getinstance (). addactivity (this);

}

Onclicklistener next_to_cclick=new Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub

Intent intent=new Intent (bactivity.this,cactivity.class);
StartActivity (Intent);

}
};

Onclicklistener exitclick=new Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Exitapplication.getinstance (). exit ();
}
};
}

Cactivity Class Source code:

Copy Code code as follows:

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Cactivity extends activity{

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);

Setcontentview (R.LAYOUT.C);

Button exit_c= (button) Findviewbyid (R.id.exit_c);
Exit_c.setonclicklistener (Exitclick);
Exitapplication.getinstance (). addactivity (this);

}

Onclicklistener exitclick=new Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Exitapplication.getinstance (). exit ();
If only one of the following methods is invoked, it does not completely exit the application
Android.os.Process.killProcess (Android.os.Process.myPid ());
System.exit (0);
}
};
}

SOURCE Download: Exitactivity.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.