16

我的活动中有这个界面。

public interface LogoutUser {
    void logout();
}

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

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

在我的活动中,我打电话

mLogoutUser.logout();

类型 LogoutUser 接口的位置在哪里mLogoutUser

我的问题mLogoutUser是空的对象。怎么初始化呢?

谢谢!

4

3 回答 3

21

正如我在评论中所说,我使用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(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");
        }
    }

    ...
}

与其他片段通信中的更多信息。


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

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

    mLogoutUser = (LogoutUser) fragment;
}

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

于 2015-08-20T23:54:54.403 回答
15

创建从 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();
    }
}
于 2017-08-30T07:07:17.407 回答
5

Android Fragments - 与 Activity 通信

您需要使用getFragmentById()or获取对您的片段的引用getFragmentByTag()

getFragmentManager().findFragmentById(R.id.example_fragment);
于 2013-05-10T22:33:50.307 回答