Show dialog only using Context instead of Activity instance

后端 未结 4 982
轻奢々
轻奢々 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 23:10

    This is one of the MOST important things that you must always remember about Contexts. There are 2 types of contexts, Activity contexts and Application contexts. You will observe in many UI related classes, a Context is passed. This is not the Application context! In such cases you must always pass an Activity Context. Except for a Toast, no other UI component will work with Application context.

    Application Context is always passed when you want some service or component which is Application related, like the Telephony Manager, Location Manager etc.

    For UIs, you must always pass a context that is UI related which is the Activity.

提交回复
热议问题