没有活动的片段 [英] Fragment without activity

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

问题描述

有人问我一个采访问题:没有活动就可以存在一个片段吗?我搜索了答案,但没有得到正确的答案和解释.有人可以帮忙吗?

I have been asked an interview question: Can a fragment exist without activity? I searched for answers but didn't get a proper answer and explanation. Can someone help?

推荐答案

是的,您可以在任何地方执行此操作:

Yes, you can do this anywhere:

new YourFragment();

因为片段必须具有无参数的构造函数.

As fragments must have a parameter-less constructor.

但是,它的生命周期直到附加之后才会生效.因此,仅在连接时调用 onAttach onCreate onCreateView 等.因此,大多数片段在附着之前都无能为力.

However its lifecycle doesn't kick in until it is attached. So onAttach, onCreate, onCreateView, etc. are only called when it is attached. So most fragments do nothing until they are attached.

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

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