如何使活动和片段的回调?回调、片段

由网友(棾硘)分享简介:我在我的活动这个接口。I have this interface in my activity.public interface LogoutUser {void logout();}我的片段实现了这个接口,所以在我的片段,我有这样的:My fragment implements this interface,...

我在我的活动这个接口。

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
}

在我的活动我称之为

mLogoutUser.logout();

其中, mLogoutUser 的类型LogoutUser接口。

Where mLogoutUser is of the type LogoutUser interface.

我的问题是, mLogoutUser 对象为空。如何初始化呢?

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

感谢您!

推荐答案

的http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

您需要得到引用您的片段,getFragmentById()或getFragmentByTag()

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

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

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

阅读全文

相关推荐

最新文章