How to Run Event before any task on Java Android?

53 Views Asked by At

I have code and worked for Exit Application with this code:

class Example extends Dialog {
    @Override
    public void dismiss() {
        super.dismiss();
        if (isForceExists != false) {
            int id = android.os.Process.myPid();
            android.os.Process.killProcess(id);
        }
    }
}

But.. when I want to running event other before exit application not working (skip event).

class ExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
       Example exDialog = new Example();
       exDialog.setDismissListener(new OnDismissListener() {
           AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
           builder.setTitle("Exit Application")
                 .setMessage("Thank you for using this application, happy nice day ^_^")
                 .show();
       });
    }
}

I want to run event dismiss or wait progress task before exit application, How to do? But it is script force exit application without running event before it?

1

There are 1 best solutions below

3
Sarah On BEST ANSWER

You should handle the event logic in your Example class or within the ExampleActivity itself, rather than trying to run the event within the onCreate method of ExampleActivity when the dialog is shown.

Modify your Example class to include a callback for the dismissal event:

class Example extends Dialog {
    private OnDismissListener dismissListener;

    public void setDismissListener(OnDismissListener listener) {
        this.dismissListener = listener;
    }

    @Override
    public void dismiss() {
        if (dismissListener != null) {
            dismissListener.onDismiss(this);
        }
        super.dismiss();
    }
}

Modify your ExampleActivity to implement the OnDismissListener interface and override its onDismiss method to execute your desired event before exiting the application:

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

public class ExampleActivity extends Activity implements DialogInterface.OnDismissListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Example exDialog = new Example(this);
        exDialog.setDismissListener(this);
        exDialog.show();
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // This method will be called before the dialog is dismissed.
        // You can perform your desired event here.
        // For example, showing a Toast message.
        Toast.makeText(this, "Before exit event executed.", Toast.LENGTH_SHORT).show();
        
        // After the event, you can exit the application if needed.
        finish();
    }
}

This approach ensures that your event runs before the application exits when you dismiss the dialog.