如何使活动和片段的回调? [英] How to make a callback between Activity and Fragment?

查看:117
本文介绍了如何使活动和片段的回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的活动这个接口。

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);

这篇关于如何使活动和片段的回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆