android - How Can I move from one fragment to new fragment? -


i trying move fragment clicking on button. current fragment named firstfragment , target fragment named attendancefragment. when run app crashed. code of current fragment

import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button;   /**  * simple {@link fragment} subclass. */ public class firstfragment extends fragment {   public firstfragment() {     // required empty public constructor }   @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     view v = inflater.inflate(r.layout.fragment_first, container, false);      button btn1 = (button) v.findviewbyid(r.id.btn1);     btn1.setonclicklistener(             new view.onclicklistener() {                  @override                 public void onclick(view v) {                      attendancefragment fragment = new attendancefragment();                     fragmentmanager fragmentmanager = getactivity().getsupportfragmentmanager();                     fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();                     fragmenttransaction.replace(r.id.register_attendance, fragment);                     fragmenttransaction.addtobackstack(null);                     fragmenttransaction.commit();                 }             }     );     return v; } 

}

make interface.

public interface fragmentchangelistener{     public void replacefragment(fragment fragment);  } 

implements parent activity interface.

public class mainactivity extends activity implements fragmentchangelistener {          @override          public void replacefragment(fragment fragment) {             fragmentmanager fragmentmanager = getsupportfragmentmanager();;                  fragmenttransaction fragmenttransaction =              fragmentmanager.begintransaction();             fragmenttransaction.replace(mcontainerid, fragment);             fragmenttransaction.addtobackstack(fragment.tostring());             fragmenttransaction.commit();        } } 

call method fragments this.

//in fragment - call method onclicklistener public void showotherfragment(){        fragment fragment=new newfragmentname();        fragmentchangelistener listener = (fragmentchangelistener)getactivity();        listener.replacefragment(fragment);  } 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -