Android application completely exits the current four methods and implementation code

Source: Internet
Author: User
Tags stub

1. Local method of Dalvik VM

Android.os.Process.killProcess (Android.os.Process.myPid ())//Get PID
System.exit (0); Normal Java, C # Standard Exit method, the return value of 0 represents a graceful exit



2. Task Manager Method

The first thing to say is that the method runs at more than 3 of the Android 1.5 API level, and requires permissions

Activitymanager am = (activitymanager) getsystemservice (Context.activity_service);
Am.restartpackage (Getpackagename ());
The system will, under the package, all processes, services, all killed, you can kill clean, you should pay attention to add
<uses-permission android:name=\ "Android.permission.restart_packages\" ></uses-permission>

3. According to the statement cycle of the activity

We know that the Android window class provides a history stack, and we can do this by using the stack principle, where we add the flag intent.flag_activity_clear_top directly to the Intent when we open the B window in window A. By opening B, all activity in the process space will be cleared.

Use the following code in window A 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);

Next in the b window you need to exit the direct use of the finish method to exit all.



4. Customizing a actiivty stack, the same is true, but using an activity stack of a single example pattern to manage all activity. and provide a way to quit all activity. The code is as follows:

public class Screenmanager {
private static stack<activity> Activitystack;
private static Screenmanager instance;
Private Screenmanager () {
}
public static Screenmanager Getscreenmanager () {
if (instance==null) {
Instance=new Screenmanager ();
}
return instance;
}
Exiting the top of the stack activity
public void popactivity (activity activity) {
if (activity!=null) {
Activity.finish ();
Activitystack.remove (activity);
Activity=null;
}
}

Get current stack top activity
Public activity currentactivity () {
Activity activity=activitystack.lastelement ();
return activity;
}

Push the current activity into the stack
public void pushactivity (activity activity) {
if (activitystack==null) {
Activitystack=new stack<activity> ();
}
Activitystack.add (activity);
}
Exits all activity on the stack
public void Popallactivityexceptone (Class cls) {
while (true) {
Activity activity=currentactivity ();
if (activity==null) {
Break
}
if (Activity.getclass (). Equals (CLS)) {
Break
}
Popactivity (activity);
}
}
}



Android completely quits application implementation code

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

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:

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:

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

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.