comment appeler le fragment kotlin à partir de la classe de fragment java
kumar
Ceci est le code ActivityUtil
public class ActivityUtil {
public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
@NonNull Fragment fragment, int frameId, String fragmentTag) {
//Fragment fragment1=fragmentManager.findFragmentByTag(fragmentTag);
Preconditions.checkNotNull(fragmentManager);
Preconditions.checkNotNull(fragment);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(frameId, fragment, fragmentTag);
transaction.addToBackStack(fragmentTag);
transaction.commit();
}
}
C'est la classe de fragment principale ici, je veux ajouter/remplacer la transaction de fragment en utilisant le fragment kotlin, c'est-à-dire la classe CameraFragment
J'ai déjà utilisé la classe Photofragment maintenant je veux changer le fragment kotlin, comment réaliser ce scénario ?
public class ExpLotBcodeFragment extends Fragment{
public ExpLotBcodeFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_exp_lot_bcode, container, false);
}
@OnClick({R.id.scan})
public void onClick(View view) {
switch (view.getId()) {
case R.id.scan:
//here i want to add kotlin call CameraFragment class
//This is old call
ActivityUtil.addFragmentToActivity(getFragmentManager(),
photoFragment, R.id.frame_content, "photoFragment");
break;
}
C'est la classe de kotlin
class CameraFragment : Fragment() {
companion object {
fun newInstance(): CameraFragment {
return CameraFragment ()
}
}
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_exp_lot_bcode, container, false)
}
}
De la classe CameraFragment, je veux ajouter/remplacer à la classe ExpLotBcodeFragment ?
Md. Asaduzzaman
Soit vous devez appeler, CameraFragment.Companion.newInstance()
soit appeler directement new CameraFragment()
pour obtenir instance
de CameraFragment
. Vérifiez ci-dessous :
@OnClick({R.id.scan})
public void onClick(View view) {
switch (view.getId()) {
case R.id.scan:
CameraFragment cameraFragment = CameraFragment.Companion.newInstance();
ActivityUtil.addFragmentToActivity(getFragmentManager(),
cameraFragment, R.id.frame_content, "cameraFragment");
break;
}
}