安卓:删除()碎片 - >再次添加()新的同一类的碎片 - > onCreateView和onActivityCreated不叫? [英] Android: remove() Fragment--> add() new Fragment of same class again ->onCreateView and onActivityCreated not called?

查看:161
本文介绍了安卓:删除()碎片 - >再次添加()新的同一类的碎片 - > onCreateView和onActivityCreated不叫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我摧毁一个编程方式创建的片段,

<$p$p><$c$c>getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.test)).commit();

这在XML文件中确定这样的:

 &LT;的LinearLayout
    机器人:ID =@ + ID /测试
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;

&LT; / LinearLayout中&GT;
 

如果我再在mainactivity重新创建从同一个类中的片段:

  getSupportFragmentManager()的BeginTransaction()
            。新增(R.id.result_bar,testinstance)
            。承诺();
 

随后的onCreate似乎没有再次调用(片段就是一句)。我在做什么错在这里?谢谢你。

解决方案

解释:为什么的FrameLayout的片段

什么是的FrameLayout

根据谷歌文档的共享布局什么的LinearLayout,RelativeLayout的,和AbsoluteLayout之间的区别?,在的ViewGroup 取值为的LinearLayout RelativeLayout的 AbsoluteLayout (decrepated) TableLayout 等都是允许显示的看法:

  1. 的LinearLayout:显示视图中的一个个
  2. RelativeLayout的:就显示另一种观点的意见
  3. TableLayout:显示在表格视图
    等等。

的FrameLayout 显示浏览用的重叠其它。人们普遍使用的包含布局:

  

帧布局是使用Android开发组织视图控制的最简单和最有效的类型的布局中的一个。它们不经常使用比某些其他布局,只是因为它们通常用来显示只有一个视图,或次相互重叠,该帧布局通常用作容器布局,因为它通常只有一个子图(经常另一个布局,用于组织多个视图)。

来源:的FrameLayout MobilTuts

  

的帧布局允许开发以仅显示内框架布局的单个或多个用户界面元素,但各元件将基于屏幕的左上角定位时,和重叠的元素,将显示重叠。

来源:的Andr​​oid框架布局对于绝对新手

确定,但是,为什么我需要这个片段? (与的LinearLayout或其中;片断&gt;)

借助的FrameLayout的谷歌文档说:

  

的FrameLayout被设计来阻挡的区域中的屏幕来显示一个单一的项目上。

的FrameLayout 将举办的布局并愿意为它。而的ViewGroup 的其余部分只是显示了意见。你可以做一个片段所有的ViewGroup 取值的(我测试了一下,这是个惊喜给我) 的,但它不会以适当的方式来做到这一点。该的FrameLayout 是:

  

...的选择,当你想重叠享有正常的布局。

如果您创建了一个℃的布局;片段... /&GT; ,您的片段会被另一个不能取代的,因为它会显示,这是附,其上视图id。要替换一个片段,则需要对其进行托管:由内的FrameLayout封装的片段,你可以只更换细节(见的这个答案)。

那么,请记住,的FrameLayout 是空盒可以承载的布局片段 S(片段的谷歌<文档/一>,它解释很简单的事实是如何使用的片段),当它们在XML中声明,他们必须有附加一个类(一个ID),其无法替代的。照片 这就是为什么我们需要一个容器以举办这次片段和重叠活动的看法!

希望这会有所帮助。

注意:如果有人想编辑这个答案,因为一些它不能解释或解释严重,她/他可以尽情地

I am destroying a programmatically created fragment with:

getFragmentManager().beginTransaction().remove(getFragmentManager().findFragmentById(R.id.test)).commit();

Which is determined in the xml file like this:

<LinearLayout
    android:id="@+id/test"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</LinearLayout>

If I then create a fragment from the same class again in the mainactivity:

getSupportFragmentManager().beginTransaction()
            .add(R.id.result_bar, testinstance)
            .commit();

Then onCreate seem not called again (the fragment is just empty). What am I doing wrong here? Thanks.

解决方案

Explanation: Why FrameLayout for Fragment

What is FrameLayout:

According to Google Documentation on Commons Layouts and this answer of What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?, the ViewGroups as LinearLayout, RelativeLayout, AbsoluteLayout (decrepated), TableLayout, etc. are allow to display views:

  1. LinearLayout: "displays views one by one"
  2. RelativeLayout: "displays views regarding to another view"
  3. TableLayout: "displays views in a table"
    etc.

FrameLayout displays views by overlap the other. It is generally use to contain layouts:

"Frame layouts are one of the simplest and most efficient types of layouts used by Android developers to organize view controls. They are used less often than some other layouts, simply because they are generally used to display only one view, or views which overlap. The frame layout is often used as a container layout, as it generally only has a single child view (often another layout, used to organize more than one view)."

source: FrameLayout MobilTuts

"The Frame layout allows developers to display only a single or multiple UI elements within Frame Layout but each element will be positioned based on the top left of the screen, and elements that overlap will be displayed overlapping."

source: Android Frame Layout For Absolute Beginner

OK but, why do I need this for Fragment? (vs LinearLayout or <fragment>)

The Google Documentation of FrameLayout says:

"FrameLayout is designed to block out an area on the screen to display a single item."

FrameLayout will host a layout and it is willing for it. Whereas the rest of ViewGroups just display views. You can make a Fragment in all ViewGroups (I tested that, it was a surprise for me) but it will not a proper way to do this. The FrameLayout are:

"...the normal layout of choice when you want to overlap views."

If you create a layout with a <fragment .../>, your fragment will be not replace by another, because it is displayed, it is "attached" with its id on the view. To replace a fragment, you need to host it: "By encapsulating the Fragment within a FrameLayout, you can replace just the details" (see this answer).

Then, keep in mind that FrameLayouts are empties and can host a layout. Fragments (Fragment Documentation from Google, it explains very simply the facts to how use a fragment), when they are declared in xml, they must to have a class attached (an id), which cannot be replaced.
That's why we need a container to host this fragment and overlap the view of the activity!

Hope this will be helpful.

Note: If someone wants to edit this answer, because something it's not explain or badly explain, she/he could with heartily.

这篇关于安卓:删除()碎片 - &GT;再次添加()新的同一类的碎片 - &GT; onCreateView和onActivityCreated不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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