如何在Activity和Fragment之间进行回调? [英] How to make a callback between Activity and Fragment?

查看:457
本文介绍了如何在Activity和Fragment之间进行回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的活动中有这个介面。

I have this interface in my activity.

public interface LogoutUser {
    void logout();
}

我的片段实现了这个接口,所以在我的片段, / p>

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 对象为null。如何初始化它?

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

谢谢!

推荐答案

Android Fragments - 与活动交流

您需要使用 getFragmentById() getFragmentByTag()

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

这篇关于如何在Activity和Fragment之间进行回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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