I have three screens (activities).Let's say A,B,C.
The screen transition is in the order A,B,C.
I want to close the application once the user tap "back" button from the third screen (screen C).
How to do it?
I have three screens (activities).Let's say A,B,C.
The screen transition is in the order A,B,C.
I want to close the application once the user tap "back" button from the third screen (screen C).
How to do it?
try the following code in c activity back button click event
System.exit(0);
or you can also use following code
android.os.Process.killProcess(android.os.Process.myPid());
As suggested by user936414, when you go from activity A to B finish activity A, when you go from B to C, finish activity B, so when you reach activity C it will be the only activity on the stack and pressing back will close it.
Like this:
startActivity(new Intent(getApplicationContext(), NextActivity.class));
finish();
You can register in every activity the following Broadcast Receiver
/* Logout Intent Actions */
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_LOGOUT");
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyApp","Loggin out from <activity>");
finish();
}
};
registerReceiver(broadcastReceiver, intentFilter);
/* Logout Intent Actions */
And then on each you just call
broadcastIntent.setAction("com.package.ACTION_LOGOUT");
sendBroadcast(broadcastIntent);
This will send a broadcast to close every activity and will put the app on background.
Remember the concept of "closing an application" in Android is quite different, as you can't actually order it to shutdown.
Use the following intent, when opening Activity C from Activity B,
Intent intent = new Intent(this, StartActivity.class)
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
It will finish all the previous activities.
When you are starting an activity C from activity B use this code to start your activity C:
Intent intent = new Intent(getApplicationContext(), ActivityC.class)
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Hope this will help you.