android - call method from fragment to Activity -
hello have activity , call many fragment based on application business need call method fragment in activity searched in internet cannot find solution
this fragment :
public class homefragment extends fragment implements view.onclicklistener { public homefragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment log.d("oncreate", "oncreateviewhf"); view = inflater.inflate(r.layout.new_fragment_home, container, false); } /// method need call in activity public void adduserlineinfofragment() { userlineinfofragment = new userlineinfofragment(); fragmenttransaction transaction = getchildfragmentmanager().begintransaction(); transaction.replace(r.id.user_info_fragment_container, userlineinfofragment).commit(); log.d("hommetest","hommmme"); }
call method of activity fragment
((youractivityname)getactivity()).adduserlineinfofragment(); call method of fragment activity
1. create interface
public interface onbuttonlistener { void onbuttonclick(); } 2. init in activity
protected onbuttonlistener onactionbuttonlistener; public void setonbuttonlistener(onbuttonlistener actionbuttonlistener) { this.onactionbuttonlistener = actionbuttonlistener; } 3. in activity, click event when action need perform
this.onactionbuttonlistener.onbuttonclick(); 4. implement listener(onbuttonlistener) in fragment
@override public void onbuttonclick(){} 5. in fragment oncreateview
((youractivityname) getactivity()).setonbuttonlistener(this);
Comments
Post a Comment