Android 如何将片段标记添加到新创建的实例中?

Android 如何将片段标记添加到新创建的实例中?,android,android-fragments,Android,Android Fragments,如何向新创建的实例添加标记 case 0: return FragmentNotifications.newInstance(notifications, mCurrentTime); 新实例: public static FragmentNotifications newInstance(String notificationsArray, long currentTime){ FragmentNotifi

如何向新创建的实例添加标记

case 0:           
 return FragmentNotifications.newInstance(notifications, mCurrentTime);
新实例:

    public static FragmentNotifications 
                 newInstance(String notificationsArray, long currentTime){

        FragmentNotifications fragment = new FragmentNotifications();
        return fragment;
    }
如何为使用viewpager创建的片段指定名称

@Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return 
             FragmentNotifications.newInstance(notifications, mCurrentTime);
            case 1:
                return FragmentMessages.newInstance(messages, 
              mCurrentTime);
            case 2:
                return new FragmentProfile();
            default:
                return null;

        }

    }

找到此方法的人的答案和积分:

我得到了由android api自动生成的标签名

@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
// get the tags set by FragmentPagerAdapter
switch (position) {
    case 0:
        String firstTag = createdFragment.getTag();
        break;
    case 1:
        String secondTag = createdFragment.getTag();
        break;
}
// ... save the tags somewhere so you can reference them later
    return createdFragment;
}
确保使用getChildFragmentManager引用它,否则它将返回null
我花了2个小时试图找出findFragmentByTag返回null的原因

你说的“标记”是什么意思?当您使用
FragmentManager
加载片段时,可以针对片段设置标记值-这就是您所指的吗?(看起来像是
getFragmentManager().beginTransaction().replace(layoutId,frag,“A_标记”).commit();
)是的,但是我如何使用新实例来完成它呢?因为我试图在viewpager中创建片段的新实例,所以我无法使用Replace。我不理解这个问题,“标记”是用于
片段管理器
,作为以后检索片段的一种方法。片段本身对它没有直接的用途。您可以将标记指定为片段中的常量值,即..
.replace(layoutId,frag,MyFragment.TAG)..
我正在尝试在viewpager中获取片段。但是当片段由我看到的viewpager(上面添加的编辑)创建时,我确定如何分配它。你在书中看到答案了吗?