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

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

问题描述

我的活动中有这个界面.

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?

谢谢!

推荐答案

Android Fragments- 与 Activity 通信

您需要使用 getFragmentById()getFragmentByTag()

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

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

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