Home > Back-end >  How to use interface to call an action from a fragment in an activity
How to use interface to call an action from a fragment in an activity

Time:01-16

I have an interface like so:

public interface IStartCamera {
    void startScanning();
}

I have a fragment that implements this interface and in this fragment I have an override for this like so:

@Override
    public void startScanning() {
        Intent intent = new Intent(getActivity(), QrScanAcitivity.class);
        startActivityForResult(intent, SQ_SCANNING_RESULT);
        startActivity(new Intent(getActivity().getApplicationContext(), QrScanAcitivity.class));
}

In my activity I create an instance of this fragment when a navigation button is clicked like so:

private IStartCamera iStartCameraInFragment;
public void setStartCameraInFragment(IStartCamera IStartCameraInFragment) {
        this.iStartCameraInFragment = IStartCameraInFragment;
    }

fragment = new ExitFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                fragment).commit();
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            fragment = null;
            switch (item.getItemId()){
                case R.id.item_manual_scanning:
                    fragment = new ExitFragment();
                    setStartCameraInFragment(fragment); // <=== I HAVE THE PROBLEM HERE
                    break;
                case R.id.item_reload:
                    fragment = new ReloadFragment();
                    break;
                case R.id.item_add_entry_info:
                    fragment = new AddEntryInfoFragment();
                    break;
            }
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    fragment).commit();
            return true;
        }



@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fabOpenCamera:
                iStartCameraInFragment.startScanning();
                break;

        }
    }

when I the setStartCameraInFragment(fragment) when instantiating the fragment, I get a wrong requirement since it is expecting an IStartCamera and not a fragment even though that fragment implements IStartCamera.

CodePudding user response:

First of all, don't instantiate the Fragment by using the constructor. It's a good practice to use a static method, usually named 'newInstance' for instantiating Fragments, because if you'd like to pass any parameter to it later on, you will be able to do it easily.

Regarding your question, you get the error because you are trying to call the 'setStartCameraInFragment' method by using a Fragment object, while the method requires an instance of IStartCamera. What you should do (if you want to stick with this logic) is check if the Fragment is an instance of the IStartCamera interface, and after that you would be able to use it as such. Here's an example:

if (fragment instanceof IStartCamera) 
     setStartCameraInFragment((IStartCamera)fragment); // --> Here the fragment can be considered as an instance of IStartCamera and this will work
  •  Tags:  
  • Related