Dialog is not displaying when pressed home to onResume state android 10-Hander issue

35 Views Asked by At

I have a button to display the Dialog. When I click the Dialog button in the sample application, immediately after a few seconds, pressing the home button. Open another application, press the home button, and open the sample application again.

Expected Result: A dialog window should be displayed.

Actual Result: A dialog window should not be displayed.

Here is the source code:

 public class DialogController {

    private static final String TAG = "DialogController";
    private static Dialog mDialog = null;

    public static void ShowErrorDialog(final Activity activity,
                                       final int messageId) {
        mDialog = new Dialog(activity);
        mDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(R.layout.layout_dialog_blue);

        TextView messageText = (TextView) mDialog.findViewById(R.id.blue_dialog_guidance);
        messageText.setText(messageId);
        messageText.setVisibility(View.VISIBLE);

        mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mDialog.setOwnerActivity(activity);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.show();

    }



}

MainActivity.java:

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    Button click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        click=(Button)findViewById(R.id.button1);

        click.setOnClickListener(v -> {
            Log.i(TAG, "clicked for dialog");

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {

                public void run() {

                    DialogController.ShowErrorDialog(MainActivity.this,
                            R.string.string_error_dialog);


                }
            }, 10000);


        });
    }
    

}

The weird thing is, the dialog window is not displaying only from Android 8 to Android 10. But this dialog window is displaying in Android 11. I am not able to figure out what issue happened.

1

There are 1 best solutions below

3
Sohaib Ahmed On

Try this,

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    Button click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        click = findViewById(R.id.button1);

        click.setOnClickListener(v -> {
            Handler handler = new Handler();
            handler.postDelayed(() -> {
                // This will be executed after 10 seconds
                getLifecycle().addObserver(new MyObserver());
            }, 10000);
        });

        
    }

    private class MyObserver implements LifecycleObserver {
        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        public void onResume() {
            // This will be called when the activity is in the resumed state
            DialogController.ShowErrorDialog(MainActivity.this, R.string.string_error_dialog);
        }
    }
}