5

I have multiple activities with 5 different screens.How should i handle a situation if a back key is pressed ??in First Screen and other screens ?? finish() and System.exit() is not working ..What should i do to exit my application if the Back button is pressed ?? The below coding does not work ?Please suggest me a way to exit an application having multiple screens

 public boolean onKeyDown(int keyCode, KeyEvent event) 
 {
    if (keyCode == KeyEvent.KEYCODE_BACK) 
     {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);

        new AlertDialog.Builder(this) .setMessage("Are you sure you want to exit?")
           .setCancelable(true)
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
           { 
             public void onClick(DialogInterface hi, int dd) 
             {

                Intent exitIntent = new Intent(Mapper.this,SplashActivity.class);
                exitIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                SplashActivity.mHandler.sendEmptyMessage(0);                   
                startActivity(exitIntent);    


             }
           }
          )
         .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
         { 
           public void onClick(DialogInterface hi, int dd) 
           {

           }
         }
         );
        AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
      //AlertDialog alert1 = alt_bld.create();
      //alert1.setTitle("EXIT");
      //alert1.show(); 
       return true; 
     }
     return super.onKeyDown(keyCode, event);
 }

  And in Start.Class:
  public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) 
    {
        mHandler.removeCallbacksAndMessages(null);
        SplashActivity.this.finish();
    }

    return super.onKeyDown(keyCode, event); 
}
Sindu
  • 215
  • 3
  • 6
  • 16
  • 3
    Alot of people tried to help you with this, and they are all right. Either accept an answer or be more specific about your question – Jameo Nov 15 '12 at 21:24

5 Answers5

6

Just override onBackPressed() like this...

@Override
public void onBackPressed()
{
    finish();  
}
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
  • I have 5 activities ...I have used tthe code above as you said ..and used finsh() after calling each activity ..but the application is going to the previous screen but not exiting..wat should i do to exit in the 2 or 3 rd screen ??I learnt tat we have pop out the activities out of the stack but how should i do tat ? – Sindu Nov 14 '12 at 22:33
  • 2
    Try `super.onBackPressed();` – Artyom Kiriliyk Nov 15 '12 at 05:49
  • 1
    If you want to close a stack of activities at once you must use `startActivityForResult(inent, R.id.your_button);` when start another activity and when you want to close activity add `setResult(RESULT_OK);` before `finish();` in onActivityResult: `@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == R.id.your_button && resultCode == RESULT_OK) { setResult(RESULT_OK); finish(); }` – Artyom Kiriliyk Nov 15 '12 at 05:56
0

What you are definitely looking for is onBackPressed:

@Override
public void onBackPressed() {
    //do your intent here
}

Im not sure from the context what :

SplashActivity.mHandler.sendEmptyMessage(0); 

This line does, but other than that your original intent should go back to your splash screen. What I would do, If you really want to exit your app on back press, I would put some data there, say

exitIntent.putExtra("toExit", true);

Basically just a flag that your splash activity can read in and decide to call finish() on itself. The only "good" way to close an app manually is to finish the first activity(although its actually pretty rare to force users to quit your app)

Edit: OK so in your app that your going back to (looks like SplashActivity), your going to look for an intent in onCreate like so:

Intent i = getIntent();
Bundle extras = i.getExtras();

Boolean toExit = extras.getString("toExit");
if(toExit){
    finish();
}
Jameo
  • 4,507
  • 8
  • 40
  • 66
  • I have used finish() after calling every activity intent..But wat it does is it returns to the before activity rather than exiting the app...What is the solution here.. – Sindu Nov 14 '12 at 22:29
  • Check out my edit to my answer. You should be calling finish in the activity your going back to as well. You will do this by putting a boolean in the intent, and then checking for it in onCreate of your first activity. If it sees the intent, it calls finish and the app should quit – Jameo Nov 15 '12 at 01:34
0

Why at all you want to exit on back button pressed? It is not android-like behavior. By pressing back user usually wants to return to previous activity, but not to exit your app. Android will handle itself activity destroying when needed.

Prizoff
  • 4,486
  • 4
  • 41
  • 69
  • As far i browsed and saw on Back pressed makes the application to exit Am i wrong / – Sindu Nov 14 '12 at 22:35
  • on back pressed will only exit your app if it is the root activity (or the first activity that you had created in your app) – Jameo Nov 15 '12 at 01:37
  • @Sindu Jameo is wright. Back usually means "return back", but not exit. Android plays different then desktop OS... here are plenty of the same questions on SO, try searching to understand how it works. – Prizoff Nov 15 '12 at 12:13
0

First, are you trying to say that you want it like this?

Activity1 --> Activity2 --> Activity3 --> Activity4 --> Activity5

Current activity is Activity5. Back pressed. Exit entire application? Do you want to exit the application on back pressed on whatever current activity you are in? Like Activity3, back pressed then exit? If yes, You can just call finish() after every startActivity() call on the caller activity. Or you may opt to use a result variable on every activity you call. For example, you can call Activity2 from Activity1 by calling it via staryActivityWithResult() and waiting for its result inside the onActivityResult() method from the source Activity - Acitivity1. So if the back is pressed on Activity2, have some variable passed on setResult() method of Activity2 before finisihing. This result will then be passed to the caller Activity1, then you may now do whatever you want depending on the result you passed.

Axel
  • 685
  • 5
  • 14
0

To exit your application at any activity. follow these steps:

1- Make an intent in the activity, from where you want to exit application:

Intent intent = new Intent(thisActivity.this, FirstActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent);

2- In your very first activity write this in onCreate() method as:

if (getIntent().getData()!= null) {

if (getIntent().getBooleanExtra("EXIT", false)) 
{
    finish();
}
}