如何使活动和片段的回调? [英] How to make a callback between Activity and 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?
感谢您!
推荐答案
的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屋!