3

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?

MidhunVP
  • 63
  • 2
  • 7
  • 3
    when starting activity B from A, just call finish() in A. similarly finish B when starting C – user936414 Jan 29 '13 at 12:20
  • This is an question that has already been asked and answer here http://stackoverflow.com/questions/6413700/android-proper-way-to-use-onbackpressed and here http://stackoverflow.com/questions/10428197/clean-stack-and-exit-app-onbackpressed – CinetiK Jan 29 '13 at 12:28
  • Google it before asking such questions -1 – Daud Arfin Jan 29 '13 at 12:30
  • You can handle this inside onActivityResult, can call finish or whatever you want !! – Daud Arfin Jan 29 '13 at 12:32
  • @CinetiK I given links didn't give a proper solution for my problems. That's why I posted the question. – MidhunVP Jan 29 '13 at 12:48

5 Answers5

4

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());
Yogesh Tatwal
  • 2,722
  • 20
  • 43
3

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();
Saito Mea
  • 455
  • 4
  • 11
1

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.

nunofmendes
  • 3,731
  • 2
  • 32
  • 42
0

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.

Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • But when I am using this from screen C when I tap back button it is moving to screen A. Then I called finish() in screen A. Then once I launch the app second time it will show the screen C. When I press the back button from screen C it is showing one blank screen. – MidhunVP Jan 29 '13 at 13:11
  • Dont use finish() in any of the activities. **FLAG_ACTIVITY_CLEAR_TOP** will automatically finish all the previous activities. – Sahil Mahajan Mj Jan 29 '13 at 13:14
0

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.

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58