shyam
shyam

Reputation: 1388

Can I close an application when a negative button on a dialog is clicked?

I have an app which uses Wifi to transfer some data. I have a dialog box appearing if the Wifi is disabled. The dialog box has two buttons: 'Yes' and 'No'. If the user clicks 'Yes', the wifi is enabled. If the user clicks 'No' the app should close itself. How can I actually make the app close on the click of the 'No' button?

Note: I use a different class (other than the MainActivity) to perform the checks on the wifi status.

Upvotes: 0

Views: 581

Answers (4)

Ultimo_m
Ultimo_m

Reputation: 4897

Your problem is that you cant access your activity from this class that you have created. The best way to deal with this problem is to use Listener interface. It allows you to capture the event when user clicks 'No' in dialog in you Activity class.

Create a interface in you class where you detect the dialog No click:

public interface DialogWiFiListener {
    public void callback(int response);
}

DialogWiFiListener mDialogWiFiListener;

public void setDialogWiFiListener (DialogWiFiListener mDialogWiFiListener) {
    this.mDialogWiFiListener = mDialogWiFiListener;
}

When you declare this class you use, you have to instantiate DialogWiFiListener object and call method setDialogWiFiListener , giving it your activity. Also inside your Activity you have to implement yourClass.DialogWiFiListener so it listens to the events.

To Finalize your work you have to call the listener object you have created when user clicks No in dialog

mDialogWiFiListener.callback(-1);// give -1 to detect his click

Then in your activity you have this:

    @Override
    public void callback(int response) { 
       if (response < 0) {
           // here you manage your work when user clicked No in dialog
           Intent startMain = new Intent(Intent.ACTION_MAIN);
           startMain.addCategory(Intent.CATEGORY_HOME);
           startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           startActivity(startMain);
           finish();
        }
    }

Upvotes: 1

Android Priya
Android Priya

Reputation: 676

another way is just pass the intent of system home screen with clear top flag. It will not force close your application but application will be closed for user.

Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);

Upvotes: 0

arielnmz
arielnmz

Reputation: 9155

You just call the finish() method of the activity/dialog you're trying to close. Killing the app can cause app lifecycle management issues.

Example:

public class MyActivity extends Activity {
    /*
    * You must call the finish() method on an instance of 
    * the activity/dialog you want to close
    */
    public void closeActivity() this.finish();

}

The actual call to the finish() method closes the application as it should, passing through all the Activity's Lifecyle corresponding methods.

Upvotes: 0

greywolf82
greywolf82

Reputation: 22193

It's not really "smart" but it should work:

android.os.Process.killProcess(android.os.Process.myPid());

Upvotes: 0

Related Questions