Skip to content Skip to sidebar Skip to footer

How To Make A Callback Between Activity And Fragment?

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 vo

Solution 1:

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:

publicclassMyFragmentextendsListFragment {
    LogoutUser mLogoutUser;

    // Container Activity must implement this interfacepublicinterfaceLogoutUser {
        publicvoidlogout();
    }

    @OverridepublicvoidonAttach(Context context) {
        super.onAttach(context);

        // This makes sure that the container activity has implemented// the callback interface. If not, it throws an exceptiontry {
            mLogoutUser = (LogoutUser) context;
        } catch (ClassCastException e) {
            thrownewClassCastException(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:

@OverridepublicvoidonAttachFragment(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 2:

Sample for creating callback from Fragment to Activity

publicinterfaceCallBackListener {
    voidonCallBack();// pass any parameter in your onCallBack which you want to return
}

CallBackFragment.class

publicclassCallBackFragmentextendsFragment {

    privateCallBackListener callBackListener;

    publicCallBackFragment() {
        // Required empty public constructor
    }

    @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_call_back, container, false);
    }

    @OverridepublicvoidonActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //getActivity() is fully created in onActivityCreated and instanceOf differentiate it between different Activitiesif (getActivity() instanceofCallBackListener)
            callBackListener = (CallBackListener) getActivity();
    }

    @OverridepublicvoidonViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button btn = (Button) view.findViewById(R.id.btn_click);
        btn.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                if(callBackListener != null)
                    callBackListener.onCallBack();
            }
        });
    }
}

CallbackHandlingActivity.class

publicclassCallbackHandlingActivityextendsAppCompatActivityimplementsCallBackListener
{
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_user);

    }

    @OverridepublicvoidonCallBack() {
        Toast.makeText(mContext,"onCallback Called",Toast.LENGTH_LONG).show();
    }
}

Solution 3:

Android Fragments - Communicating with Activity

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

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

Post a Comment for "How To Make A Callback Between Activity And Fragment?"