Google's official recommendation is to use setargument to send arguments instead of using constructors.
Because the fragment will call its own parameterless constructor when switching the screen, the arguments in the constructor will be invalidated.
public class Framenttestactivity extends Actionbaractivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
After switching the screen after the savedinstancestate is not NULL, so only the first time in the call, if not judged, switch the screen will be in the original fragment on the basis of a new fragment, so that there are 2 fragment overlap.
if (savedinstancestate = = null) {
Getsupportfragmentmanager (). BeginTransaction ()
. Add (R.id.container, New testfragment ("param")). commit ();
}
}
public static class Testfragment extends Fragment {
Private String MArg = "Non-param";
Public Testfragment () {
LOG.I ("INFO", "Testfragment Non-parameter constructor");
}
Public testfragment (String Arg) {
MARG = arg;
LOG.I ("INFO", "testfragment construct with parameter");
}
@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
View Rootview = inflater.inflate (R.layout.fragment_main, container,
FALSE);
TextView TV = (TextView) Rootview.findviewbyid (r.id.tv);
Tv.settext (MARG);
return rootview;
}
}
}
Use Fragment.setarguments (bundle bundle) to pass parameters instead of constructors