Three ways to completely exit Android programs

Source: Internet
Author: User

Many users may find their Android program has a lot of activity, such as the main window A, called sub-window B, in b How to shut down the entire Android application? Here Android123 give you three kinds of relatively simple way to achieve.

First of all to explain the direct use of the finish in B (), and then the phone shows the main window A, so together to see how the Android Development network is implemented.

1. Local methods for Dalvik VMS

Android.os.Process.killProcess (Android.os.Process.myPid ())//Get PID, currently get its own also only the API, otherwise from/PROC in its own enumeration of other processes it, but to illustrate that, Ending other processes does not necessarily have permissions, or it is a mess.
System.exit (0); Normal Java, C # Standard Exit method with a return value of 0 for graceful exit

2. Task Manager method

First of all to demonstrate that the method runs on Android 1.5 API level is more than 3 can, while the need for permission Android.permission.RESTART_PACKAGES, we directly end their package, Directly using the Activitymanager class's Restartpackage method, the parameter is package name, which is passed Getsystemservice (context.activity_service) To instantiate the Activitymanager object, which is provided by the system but needs to be displayed with declarative permissions, so it needs to be considered in a comprehensive way.

3. According to the activity's declaration cycle

We know that the Android window class provides a history stack, which we can skillfully implement using the stack principle, where we add the flag intent.flag_activity_clear_top directly to the intent when opening the b window in a window. When you turn on B, all activity for that process space will be cleared.

In the a window, use the following code to invoke the b window

Intent Intent = new Intent ();
Intent.setclass (Android123.this, Cwj.class);
Intent.setflags (Intent.flag_activity_clear_top); Note the flag setting of the bank
StartActivity (Intent);

Often need to quit an Android application, if the application is a lot of activity, directly with the finish () can only quit the current activity, but not completely exit, although there are many ways to implement the online, I also give you a way to introduce a realization. For example, now that I have n activity, and every activity inside can use the menu key to exit the application, what should I do? Does every activity have a menu written in it? Of course not, we can define a general baseactivity, the activity of course inherits the most basic activity, and we implement the menu function in the baseactivity, realizes the program exit, The other activity in the program is then inherited to the baseactivity.

The code is as follows:

Package com.apkstory;

Import java.util.ArrayList;

Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.DialogInterface;
Import Android.content.DialogInterface.OnClickListener;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;

public class Baseactivity extends Activity {

public static final String TAG = "baseactivity";
public static arraylist<activity> activitylist = new arraylist<activity> ();

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Activitylist.add (this);
LOG.I (TAG, activitylist.tostring ());
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Menu.add (0, 0, 0, "exit program");
return Super.oncreateoptionsmenu (menu);
}

@Override
public boolean onoptionsitemselected (MenuItem item) {
if (item.getitemid () = = 0) {
Exitapp ();
}
return super.onoptionsitemselected (item);
}

@Override
protected void OnDestroy () {
Super.ondestroy ();
Activitylist.remove (this);
LOG.I (TAG, activitylist.tostring ());
}

/**
* Completely quit the app
*/
public void Exitapp () {
Alertdialog.builder Builder = new Alertdialog.builder (this);
Builder.settitle ("hint");
Builder.setmessage ("Are you sure you want to exit the program?");
Builder.setpositivebutton ("OK", new Onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {
if (activitylist.size () &gt; 0) {
for (Activity activity:activitylist) {
Activity.finish ();
}
Android.os.Process.killProcess (Android.os.Process.myPid ());
}
}
});
Builder.setnegativebutton ("Cancel", null);
Builder.show ();
}

}

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

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

public class Exitapplication extends application {


Private list<activity> activitylist=new linkedlist<activity> ();

private static exitapplication instance;

Private Exitapplication ()
{
}
Get a unique exitapplication instance in singleton mode
public static exitapplication getinstance ()
{
if (null = = instance)
{
Instance = new Exitapplication ();
}
return instance;

}
Adding 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 are simple examples, respectively, are
Indexactivity–>bactivity–>cactivity The jump order. In each activity class, the OnCreate () method
Use the Exitapplication.getinstance (). Addactivity (activity activity) method. At any one of the activity interfaces
The Exitapplication.getinstance (). Exit () method can be used whenever an activity
Exit the application completely.
Indexactivity Class Source code:
View Plaincopy to Clipboardprint?
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 when the activity is first created. */
@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:

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:

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 you just call one of the following methods, the app does not exit completely
Android.os.Process.killProcess (Android.os.Process.myPid ());
System.exit (0);
}
};

}

Three ways to completely exit Android programs

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.