获取当前片段对象 [英] Get the current fragment object

查看:118
本文介绍了获取当前片段对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的的main.xml

 <的FrameLayout
        机器人:ID =@ + ID / frameTitle
        机器人:填充=5DP
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =FILL_PARENT
        机器人:背景=@可绘制/ title_bg>
            <片段
              机器人:名称=com.fragment.TitleFragment
              机器人:ID =@ + ID / fragmentTag
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =WRAP_CONTENT/>

  < /的FrameLayout>
 

像这样的

和我设置片段对象

  FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
片段newFragment =新FragmentType1();
fragmentTransaction.replace(R.id.frameTitle,casinodetailFragment,fragmentTag);

// fragmentTransaction.addToBackStack(空);
fragmentTransaction.commit();
 

这是设置不同类型的片段的对象(FragmentType2,FragmentType3,... <$ C C $>)在不同的时间。现在,在一些时间点,我需要确定哪些对象当前存在。

我需要做的是这样的:

 片段currentFragment = //什么方式来获得当前片段对象的FrameLayout R.id.frameTitle
 

我尝试了以下

  TitleFragment titleFragmentById =(TitleFragment)fragmentManager.findFragmentById(R.id.frameTitle);
 

  TitleFragment titleFragmentByTag =(TitleFragment)fragmentManager.findFragmentByTag(fragmentTag);
 

两个对象(titleFragmentById和titleFragmentByTag)是
我错过了什么?
我使用兼容包,R3 和制定 API级别7

findFragmentById() findFragmentByTag()如果我们使用设置片段将工作 fragmentTransaction.replace fragmentTransaction.add ,但返回NULL 如果我们已经设置了对象在XML(像我在做我的的main.xml )。我觉得我失去了一些东西在我的XML文件中。

解决方案
  

现在在一些时间点,我需要确定哪些对象当前有

呼叫 findFragmentById() FragmentManager 并确定哪些片段是在你的研究。 id.frameTitle 容器。

In my main.xml I have

  <FrameLayout
        android:id="@+id/frameTitle"
        android:padding="5dp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="@drawable/title_bg">
            <fragment
              android:name="com.fragment.TitleFragment"
              android:id="@+id/fragmentTag"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" />

  </FrameLayout>

And I'm setting fragment object like this

FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new FragmentType1();
fragmentTransaction.replace(R.id.frameTitle, casinodetailFragment, "fragmentTag");

// fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

It is setting different types of Fragment objects (FragmentType2,FragmentType3,...) at different time. Now at some point of time I need to identify which object is currently there.

In short I need to do something like this:

Fragment currentFragment = //what is the way to get current fragment object in FrameLayout R.id.frameTitle

I tried the following

TitleFragment titleFragmentById = (TitleFragment) fragmentManager.findFragmentById(R.id.frameTitle);

and

    TitleFragment titleFragmentByTag = (TitleFragment) fragmentManager.findFragmentByTag("fragmentTag");

But both the objects (titleFragmentById and titleFragmentByTag ) are null
Did I miss something?
I'm using Compatibility Package, r3 and developing for API level 7.

findFragmentById() and findFragmentByTag() will work if we have set fragment using fragmentTransaction.replace or fragmentTransaction.add, but will return null if we have set the object at xml (like what I have done in my main.xml). I think I'm missing something in my XML files.

解决方案

Now at some point of time I need to identify which object is currently there

Call findFragmentById() on FragmentManager and determine which fragment is in your R.id.frameTitle container.

这篇关于获取当前片段对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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