I am trying to implement a general dialog method that every activity can call it. The public static method shall be something like this. But I have problem to cast mContent to its activity class name.
public static void openDialogEntry(Class activityClassName, Context mContext, String title, ... ) {
Dialog_Entries dialog = new Dialog_Entries( title, ... );
dialog.show( ((activityClassName) mContext).getSupportFragmentManager(), tag);
}
I want to call this method from fragments of any activity for example from a fragment(view) of SecondActivity.java using
openDialogEntry(view.getContext().getClass, view.getContext(), title, ... ) ;
What I expect to do is doing something like next line in openDialogEntry method
dialog.show( ((SecondActivity) mContext).getSupportFragmentManager(), tag);
It seems that ((activityClassName) mContext) is not working.
I want to call the public static method from MainActivity.java and ThirdActivity.java too (and their fragments which would need the casting).
How to realize this goal ?
Class has a method
Class.cast(Object)that is you could do this:Note that
castonly makes sense when providing generic information on theClassparameterHowever as @Zabuzard already mentioned. This makes not much sense. If you can cast
mContexttoactivityClassNamewhy not use this class instead ofContextas method parameter?Contextprobably implements someinterfaceor extends someclassthat provides thegetSupportFragmentManagermethod. So there is no need to cast at all.