Issue
I'm trying to call this function public void arrowClick()
is inside my main fragment public class CounterMain extends Fragment implements View.OnClickListener{
the fragment is extend android.support.v4.app.Fragment
I want to call this function from another Custmoe Dialog fragment
public class CustmoeDialog extends DialogFragment {
I tried ((CounterMain)getActivity()).arrowClick();
but I can't use it it says android.support.v4.app.Fragment cannot be cast to my.example.CounterMain
and the
CounterMain x = new CounterMain(); x.arrowClick();
it makes my app to stop working when I call it
any help?
Solution
You can call activity method by this way
((YourActivityClassName)getActivity()).yourPublicMethod();
and from activity you can directly call by this way
FragmentManager fm = getSupportFragmentManager();//if added by xml
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();
if you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead:
YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");
Answered By - Swap-IOS-Android
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.