0

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 Answer 1

0

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); } } } 
Sign up to request clarification or add additional context in comments.

3 Comments

I don't want to use AppCompatActivity.
No. LifecycleObserver not supported in Activity
You need to implement dependency of "lifecycle-runtime-ktx" from "developer.android.com/jetpack/androidx/releases/…"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.