Complete Exit program in Android (quit all activity) _android

Source: Internet
Author: User

This is a lot of people will encounter problems, I have tried a lot of methods, are not easy to use.
Like what:

Copy Code code as follows:

System.exit (0);

No way.
What else to jump to the first activity, while the top of the stack of activity all clear, and finally finish (); Don't know why.
Here is a method of my own, the effect is very good.
Principle: Registers a broadcast receiver at each activity to receive broadcasts to close the activity. When you need to exit the program, send a broadcast that closes the activity so that all the activity is received, and then all the activity will be done by itself.

Copy Code code as follows:

Package Com.example.exitsystem;

Import android.app.Activity;
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.IntentFilter;
Import Android.os.Bundle;

/**
* All activity inherits this class and is equal to registering the broadcast,
* When you need to completely exit the system, you can send the broadcast,
* Action is com.example.exitsystem.system_exit (custom),
* So we can quit all the activity at any time.
* @author Linzhiquan
*
*/
public class Superactivity extends activity {
/** Broadcast Action *
public static final String system_exit = "Com.example.exitsystem.system_exit";
/** Receiver *
Private Myreceiver receiver;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

Registering a broadcast for exiting a program
Intentfilter filter = new Intentfilter ();
Filter.addaction (System_exit);
Receiver = new Myreceiver ();
This.registerreceiver (receiver, filter);
}

@Override
protected void OnDestroy () {
Remember to cancel the broadcast registration
This.unregisterreceiver (receiver);
Super.ondestroy ();
}

Private class Myreceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
Finish ();
}
}
}


Copy Code code as follows:

Package Com.example.exitsystem;

Import Android.os.Bundle;

/**
* Ordinary activity, inherit superactivity
* @author Linzhiquan
*
*/
public class Mainactivity extends Superactivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}
}


Send a broadcast when you need to exit the program.

Copy Code code as follows:

Intent Intent = new Intent ();
Intent.setaction (Superactivity.system_exit);
Sendbroadcast (Intent);

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.