android - fragment callback using interface -
i have activity
starts fragmen
t. inside fragment have edittext
. , when text user types there, want activity
of interface
. i'm using this guide
on mainactivity
i'm implementing commentlistener interfac
e , i've managed result inside oncommententered
method. on donelistener
, triggered when user presses button finishing activity, i'm getting null.
obviously oncommententered
runs after donelistener
.
any suggestions on how result on donelistener
?
class mainactivity implements fragment.commentlistener{ static string comment; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.addtransaction); done=(textview)findviewbyid(r.id.done_button); } // called when user presses button , mainactivity finishes. public void donelistener(view v){ // here null system.out.println(comment); finish(); } @override public void oncommententered(string data) { comment=data; // here user typed system.out.println(comment); } }
my fragment
public class thefragment extends fragment { commentlistener clistener; static textview note; edittext comment; public interface commentlistener{ void oncommententered(string data); } public static thefragment newinstance(){ return new thefragment (); } public thefragment (){ } @override public void onattach(context context) { super.onattach(context); clistener=(commentlistener) context; } @override public void ondetach() { super.ondetach(); clistener=null; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v; v=inflater.inflate(r.layout.fragment, container, false); comment=(edittext)v.findviewbyid(r.id.comment_picker); comment.setonfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view v, boolean hasfocus) { if (!hasfocus) { clistener.oncommententered(comment.gettext().tostring()); } } }); return v; } }
create new interface, implement same in fragment. create method in interface , override same in fragments. while calling fragment activity create interface type fragment , call interface method.
eg:
public class thefragment extends fragment implement fragmentnofyinterface { ... @override protected void notify(string txt){ mtvtxt.settext(txt); } ..... } interface format public interface fragmentnofyinterface { protected void notify(string txt); }
activity format
class mainactivity implements fragment.commentlistener{ ..... private fragmentnofyinterface mfragmentnotifier; ......... thefragment mfragment = new thefragment(); mfragmentnotifier = (fragmentnofyinterface ) mfragment; fragmenttransaction transaction = mfragmentmngr.begintransaction(). add(r.id.rl_fragment_navigation_container, mfragment); transaction .commit(); ...... //notify fragment when required mfragmentnotifier.notify("hello world"); }
Comments
Post a Comment