How to make a callback between Activity and Fragment?

53,405

Solution 1

Android Fragments - Communicating with Activity

You need to get a reference to your fragment with getFragmentById() or getFragmentByTag()

getFragmentManager().findFragmentById(R.id.example_fragment);

Solution 2

As I said in my comment, I resolved this issue using onAttach method in my fragment, but in this way you have to have the callback field (mLogoutUser in this case) declared in the fragment, and initialize it this way:

public class MyFragment extends ListFragment {
    LogoutUser mLogoutUser;

    // Container Activity must implement this interface
    public interface LogoutUser {
        public void logout();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mLogoutUser = (LogoutUser) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement LogoutUser");
        }
    }

    ...
}

More info in Communicating with Other Fragments.


But if your case is the field declared in the activity, you can use the onAttachFragment method from your activity to initialize your listener field this way:

@Override
public void onAttachFragment(Fragment fragment) {
    super.onAttachFragment(fragment);

    mLogoutUser = (LogoutUser) fragment;
}

Also, you can use an event bus to make this communication between fragments and activities. An option is the Otto library, from Square.

Solution 3

Sample for creating callback from Fragment to Activity

public interface CallBackListener {
    void onCallBack();// pass any parameter in your onCallBack which you want to return
}

CallBackFragment.class

public class CallBackFragment extends Fragment {

    private CallBackListener callBackListener;

    public CallBackFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_call_back, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //getActivity() is fully created in onActivityCreated and instanceOf differentiate it between different Activities
        if (getActivity() instanceof CallBackListener)
            callBackListener = (CallBackListener) getActivity();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button btn = (Button) view.findViewById(R.id.btn_click);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(callBackListener != null)
                    callBackListener.onCallBack();
            }
        });
    }
}

CallbackHandlingActivity.class

public class CallbackHandlingActivity extends AppCompatActivity implements CallBackListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_user);

    }

    @Override
    public void onCallBack() {
        Toast.makeText(mContext,"onCallback Called",Toast.LENGTH_LONG).show();
    }
}
Share:
53,405
androidevil
Author by

androidevil

I am an Android developer/user/lover.

Updated on February 20, 2020

Comments

  • androidevil
    androidevil about 4 years

    I have this interface in my activity.

    public interface LogoutUser {
        void logout();
    }
    

    My fragment implements this interface, so in my fragment, I have this:

    @Override
    public void logout() {
        // logout
    }
    

    In my activity I call

    mLogoutUser.logout();
    

    Where mLogoutUser is of the type LogoutUser interface.

    My issue is the mLogoutUser object that is null. How can initialize it?

    Thank you!