I have seen two general practices to instantiate a new Fragment in an application:
Fragment newFragment = new MyFragment();
and
<
I'm lately here. But somethings I just known that might help you a bit.
If you are using Java, there is nothing much to change. But for kotlin developers, here is some following snippet I think that can make you a basement to run on:
inline fun newInstance(text: String): T {
return T::class.java.newInstance().apply {
arguments = Bundle().also { it.putString("key_text_arg", text) }
}
}
val f: SampleFragment = SampleFragment.newInstance("ABC")
// or val f = SampleFragment.newInstance("ABC")
fun newInstance(): ChildSampleFragment {
val child = UserProfileFragment.newInstance("XYZ")
// Do anything with the current initialized args bundle here
// with child.arguments = ....
return child
}
Happy coding.