Alert Dialog in non activity class

前端 未结 4 2166
南旧
南旧 2021-01-26 02:37

I have a code which checks some data and shows an alert in a non activity class. But while running application program crashed and does not showing alert dialog. I used below

4条回答
  •  灰色年华
    2021-01-26 03:30

    You simply cannot show an AlertDialog using application's context. Dialogs are supposed to attached to an activity's window using its context and stay alive inside its life-cycle. Therefore, you simply need to provide an activity's context which will then allow you to show a Dialog.

    As an alternate, I would suggest you to show an Activity themed as dialog using android:theme="@android:style/Theme.Dialog" in your AndroidManifest.xml. This will serve the purpose of manipulating a dialog but in real it will be an activity. As an advantage, you can launch your activities anywhere in your code as long as you have access to application's context.

    For more info, read this answer.

提交回复
热议问题