Show dialog only using Context instead of Activity instance

后端 未结 4 986
轻奢々
轻奢々 2021-02-04 22:36

I could show dialog if I uses an Activity instance but when I uses Context or Application Context instance Dialog is not showing.

AlertDialog.Builder builder = n         


        
4条回答
  •  日久生厌
    2021-02-04 22:50

    The problem is something I faced recently too, you cant create a dialog without and activity instance. getApplicationContext() call doesn't work too. The way I did this is to make the call to a method that creates the dialog, from an activity, and pass "this" i.e. the reference to that activity as a parameter.

    If you are going to reuse this code, as a reusable component or as a mechanism to create dialogs at multiple places, create a base activity class and have this method in there, and use it in sub-classed activities as needed.

提交回复
热议问题