passing context as argument of DialogFragment

后端 未结 4 2096
挽巷
挽巷 2021-02-01 03:15

it\'s possible to pass a context variable to a DialogFragment?

i\'ve use this code inside dialog for passing a string:

public static ConfirmDialog newIns         


        
相关标签:
4条回答
  • 2021-02-01 03:50
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        context=activity;
    }
    

    Need to use onAttach method : for dialog Fragment

    0 讨论(0)
  • 2021-02-01 04:01

    Your DialogFragment has a very handy method for getting a Context instance:

    getActivity()
    

    Fragment#getActivity() will return the instance of the Activity (which is a Context) that the Fragment is attached to. Use it after the Fragment's onAttach() is called. The below chart illustrates the Fragment lifecycle, as you can see, using getActivity() from onCreate() to onDestroy() should be a valid call.

    enter image description here

    For more information, read the Fragment documentation

    0 讨论(0)
  • 2021-02-01 04:07

    onAttach(Activity activity) is now deprecated,

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
    


    use onAttach(Context context) instead

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
    
    0 讨论(0)
  • 2021-02-01 04:17

    use like this:

    public class Dialog extends DialogFragment implements OnClickListener {
        public void onClick(View v) {
        switch (v.getId()) {
            case R.id.message: {
                this.startActivity(new Intent(context, Login.class));
                                     //or use getActivity() instead of context
                }
                break;
             }
        }
        @Override
        public void onAttach(Activity activity) {
            // TODO Auto-generated method stub
            super.onAttach(activity);
            context=activity;
        }
    }
    
    0 讨论(0)
提交回复
热议问题