如何在Activity和Fragment之间进行回调?

时间:2013-05-10 21:50:21

标签: android interface android-activity callback fragment

我的活动中有这个界面。

public interface LogoutUser {
    void logout();
}

我的片段实现了这个接口,所以在我的片段中,我有这个:

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

在我的活动中,我致电

mLogoutUser.logout();

其中mLogoutUser的类型为LogoutUser接口。

我的问题是mLogoutUser对象为null。如何初始化它?

谢谢!

3 个答案:

答案 0 :(得分:13)

正如我在评论中所说,我在我的片段中使用onAttach方法解决了这个问题,但是这样你必须在片段中声明回调字段(在这种情况下为mLogoutUser)并初始化它这样:

public class MyFragment extends ListFragment {
    LogoutUser mLogoutUser;

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

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

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

    ...
}

Communicating with Other Fragments中的更多信息。

但是如果您的案例是活动中声明的字段,您可以使用活动中的onAttachFragment方法以这种方式初始化您的侦听器字段:

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

    mLogoutUser = (LogoutUser) fragment;
}

此外,您可以使用事件总线在片段和活动之间进行此通信。一个选项是来自Square的Otto library

答案 1 :(得分:7)

  

用于创建从Fragment到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();
    }
}

答案 2 :(得分:4)

Android Fragments - Communicating with Activity

您需要使用getFragmentById()getFragmentByTag()

来获取对您的片段的引用
getFragmentManager().findFragmentById(R.id.example_fragment);