-1

i wrote alertdialog and i try to exit app in ok click,i wrote some code but when i click OK button app starting again automaticly this is a my source

public class AlertDialogManager {

public void showAlertDialog(Context context, String message) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    alertDialog.setMessage(message);


    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
             android.os.Process.killProcess(android.os.Process.myPid());

             System.exit(1);

        }
    });


    alertDialog.show();
}

i call this class like this but i can't close app in OK click

    if (!con.isConnectingToInternet()) {
        alert.showAlertDialog(getActivity(),
                "You have not internet connection");
    } else {
        mainthread.execute();
    }

P.s i checked internet connection.it's working perfect.do't worry :)

what am i doing wrong? if anyone knows solution please help me

BekaKK
  • 2,173
  • 6
  • 42
  • 80
  • Don't put exit in Android apps unless you need to stop a background process. It's annoying and not needed. – Simon Aug 25 '14 at 14:57
  • possible duplicate of [How to quit android application programmatically](http://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically) – calimbak Aug 25 '14 at 14:57
  • 4
    `Process.killProcess(android.os.Process.myPid());` and `System.exit(1);` are among the worst things you can do to your app. This won't just exit your app, this will f**k it up. **Never** call those methods, just use `finish()`. The Android OS decides when to start/stop apps. – Xaver Kapeller Aug 25 '14 at 14:57
  • If you want to quit with an error, then throw a RuntimeException. – EpicPandaForce Aug 25 '14 at 14:58
  • guys i try to check internet connection and i use this class.for more information,when internet connection is not valid i try to show this alert message – BekaKK Aug 25 '14 at 15:00
  • 2
    So show the alert but why exit? It is not needed and is bad. – Simon Aug 25 '14 at 15:02
  • @user3863447 Yeah, we all got what you are trying to do from your code, it's not that difficult to understand, but if you want to close the `Activity` if there is no internet connection then use `finish()`, but not `Process.killProcess()` or `System.exit()`. And just for the record: Making your app unusuable if there is no internet connection is bad... Just show cached values or some error message indicating that he user needs an internet connection to continue, but don't just force the user to leave, that's terrible UX. – Xaver Kapeller Aug 25 '14 at 15:06
  • @Xaver Kapeller first,i can't write finish() in my AlertDialog class and second,i have AsyncTask class and when internet is aviable then i want to use my asynctask.(simple parse json and show it).i have question for yout.first how i can solve my problem and second what will be better way to tell users about internet connection? thanks .i hope,you'll help me – BekaKK Aug 25 '14 at 15:11
  • @user3863447 just cause there is no internet connection does not mean you should exit your app. Telling the user that they lost internet is great but let them continue using the app. How would you like it if you could not use an app everytime you lost internet, you probably wouldnt. If you have stuff that needs the internet you should handle that aka not execute that code and tell the user why. unles you have a valid reason for not wanting to let the user use the app unless they have internet like security reasons, never exit the app manually – tyczj Aug 25 '14 at 15:14
  • @user3863447 Well adding a call to `finish()` to your `AlertDialogManager` would also be just as bad, I didn't mean that. But what you can do is pass the click listener into `showAlertDialog()` making your `AlertDialogManager` much more versatile. In that callback you can easily call `finish()`. And you can very well display and `AlertDialog` to tell the user that he needs an internet connection, or you can also use a `Toast` or display that information somewhere else, it all depends on your use-case. But one should never make the user do anything if he wants to leave than that's his decision. – Xaver Kapeller Aug 25 '14 at 15:15

3 Answers3

1

as the info on this link says:

There is no application quitting in android, SampleActivity.this.finish(); will finish the current activity.

When you switch from one activity to another keep finish the previous one

Intent homeintent = new Intent(SampleActivity.this,SecondActivity.class);
startActivity(homeintent);
SampleActivity.this.finish();

EDIT

or instead just use finish() method but it will just close the actual activity...

Community
  • 1
  • 1
geekCode
  • 336
  • 2
  • 11
0

Instead of killing process use finishAffinity(), which will close/clear all the activities with same affinity from back stack and exit out of the application.

public void onClick(DialogInterface dialog, int which)
    this.finishAffinity(); 
    System.exit(1);
}
Idan
  • 5,405
  • 7
  • 35
  • 52
Godugu
  • 1
  • 1
-1

If you dialog is activity class then just call

finish();

Otherwise call using context

((Activity)context).finish();
Archit Jain
  • 487
  • 1
  • 3
  • 12