Show dialog only using Context instead of Activity instance

后端 未结 4 983
轻奢々
轻奢々 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.

    0 讨论(0)
  • 2021-02-04 23:03

    For some reason [at least in android 2.1] a toast can be on the application context but not a progress dialog

    MyActivity.this is an activity specific context which does not crash

    MyActivity.getApplicationContext() is global and will crash progress bars and in later versions also toasts.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 23:10

    To use a context, it has to be passed from the activity on which the dialog is being created from. And this is only possible when you create your own dialog instead of using the AlertDialog.Builder provided by Java. Here is a custom one and we shall use it to get the application context.

    public class CustomDialogPopUp extends Dialog implements View.OnClickListener {
    
    public Activity a;
    public Context c;
    public Dialog d;
    //CUSTOMIZE YOUR DIALOG AS YOU LIKE
    RecyclerView pointAbsorber;
    ImageButton addPoint, attachPoint;
    EditText addContent;
    
    //VERY IMPORTANT CONSTRUCTOR THAT WE SHALL USE TO GET THE CONTEXT
    public CustomDialogPopUp(@NonNull Context context, Activity a, Context c) {
        super(context);
        this.a = a;
        this.c = c;
    }
    
    //INITIALIZING YOUR CUSTOM DIALOG VIEWS AND WHAT NOT. IT NEEDS AN XML FILE BY THE WAY
    public void initViewSnActions(){
        pointAbsorber = findViewById(R.id.pointsRV);
        addPoint = findViewById(R.id.addPoint);
        attachPoint = findViewById(R.id.attachPoint);
        addContent = findViewById(R.id.addContent);
        
    }
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        initViewSnActions();
    }
    
    //ON CLICK LISTENER FOR YOUR VIEWS
    @Override
    public void onClick(View v) {
        
    }
    

    }

    To pass the application context, one has to initialize it in whichever class you want it to appear as follows. Note how the activity and the context is passed

    NotesPopUp notesPopUp = new NotesPopUp(PersistentTest.this, getParent(), getApplicationContext());
    notesPopUp.show();
    
    0 讨论(0)
提交回复
热议问题