android - Calling a fragment method inside a custom view -
so background i've tried call method have in fragment (that's hosting custom view) within custom view.
i've tried using interface , implementing in fragment. i've tried getting instance of fragment in customview , calling method way. whatever reason, null pointers both ways.
way #1
public surveyview(context context, attributeset attrs) { super(context, attrs); setorientation(vertical); options = new arraylist<>(); //updatepolls interface. updatepolls = new multiplechoicepollfragment(); } public interface updatepolls { void update(); }
then call method in onclicklistener:
v.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { (int p = 0; p < options.size(); p++) { if (options.get(p) == v) { setanswers(p); updatepolls.update(); } } } });
the method update() gets called in fragment , try display toast, returns null pointer.
way #2
get instance of fragment in customview's constructor , call method.
public surveyview(context context, attributeset attrs) { super(context, attrs); setorientation(vertical); options = new arraylist<>(); myactivity = (appcompatactivity)context; myactivity.getfragmentmanager().findfragmentbytag("multiple_choice_poll_fragment"); myactivity.update(); }
this returns null pointer....
my fragment is:
public class multiplechoicepollfragment extends fragment implements surveyview.updatepolls
and method implement is:
@override public void update() { toast.maketext(getactivity(), "success", toast.length_short).show(); }
myactivity = (appcompatactivity)context; myactivity.getfragmentmanager().findfragmentbytag("multiple_choice_poll_fragment");
this work, need use getsupportfragmentmanager()
.
Comments
Post a Comment