如何在Android中将标签设置为片段 [英] How to set a Tag to a Fragment in Android

查看:54
本文介绍了如何在Android中将标签设置为片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了关于Stackoverflow的所有问题,但找不到该问题的单个明确答案.如何将标签设置为 Fragment ,以便可以通过 getFragmentManager().findFragmentByTag()对其进行检索?有人可以举一个简单的代码示例来说明如何为 Fragment 创建标签吗?

I've looked at all the questions on Stackoverflow but could not find a single definitive answer to this question. How do you set a Tag to a Fragment so that you can retrieve it via getFragmentManager().findFragmentByTag()? Could someone give a simple code example of how to create a tag to a Fragment?

推荐答案

您可以在片段交易期间设置 Tag .

You can set a Tag during fragment transaction.

例如,如果这是一个 replace 交易,则可以这样操作:

For example if it's a replace transaction you could do it like so:

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
        .replace(R.id.fragment_container, mFragment, TAG)
        .commit();

如果您使用的 Fragment 不是支持库中的不是,请使用 getFragmentManager()而不是 getSupportFragmentManager().

If the Fragment you're using is not from Support Library, use getFragmentManager() instead of getSupportFragmentManager().

这篇关于如何在Android中将标签设置为片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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