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