1

This is ActivityUtil code

        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();


        }
        }

This is main fragment class here i want add /replace fragment transaction by using kotlin fragment i.e,CameraFragment Class

I Already used Photofragment class now i want change the kotlin fragment, how do achieve this scenario?

    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;
    }

This is kotlin class

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)
  }

}

From CameraFragment class i want to add/replace to ExpLotBcodeFragment class ?

5
  • Not clear about your question Commented Oct 23, 2019 at 19:08
  • I have android app with java class name as photofragment and in the same app I have created kotlin fragment name as camerafragment . I want load the photo fragment instead of camera fragment Commented Oct 24, 2019 at 15:26
  • How do the fragment transaction between java fragment and kotlin fragment Commented Oct 24, 2019 at 15:50
  • Add both fragment and ActivityUtil code Commented Oct 24, 2019 at 15:54
  • updated the code Commented Oct 24, 2019 at 18:08

1 Answer 1

1

Either you have to call CameraFragment.Companion.newInstance() or directly call new CameraFragment() to get instance of CameraFragment. Check below:

@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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

My Question is how to call from ExpLotBcodeFragment (Java ) to CameraFragment (Kotlin)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.