Passing Context As Argument Of Dialogfragment
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 newInstance( String f) { Confir
Solution 1:
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.
For more information, read the Fragment
documentation
Solution 2:
@OverridepublicvoidonAttach(Activity activity) {
// TODO Auto-generated method stubsuper.onAttach(activity);
context=activity;
}
Need to use onAttach method : for dialog Fragment
Solution 3:
onAttach(Activity activity) is now deprecated,
@OverridepublicvoidonAttach(Activity activity) {
super.onAttach(activity);
}
use onAttach(Context context) instead
@OverridepublicvoidonAttach(Context context) {
super.onAttach(context);
}
Solution 4:
use like this:
publicclassDialogextendsDialogFragmentimplementsOnClickListener {
publicvoidonClick(View v) {
switch (v.getId()) {
case R.id.message: {
this.startActivity(newIntent(context, Login.class));
//or use getActivity() instead of context
}
break;
}
}
@OverridepublicvoidonAttach(Activity activity) {
// TODO Auto-generated method stubsuper.onAttach(activity);
context=activity;
}
}
Post a Comment for "Passing Context As Argument Of Dialogfragment"