gpt4 book ai didi

android - 一项 Activity 或单独 Activity 中的多个 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:46 24 4
gpt4 key购买 nike

我一直在读这个article (顺便说一句)它说明了这一点

To create different layouts with fragments you can:

Use one activity, which displays two fragments for tablets and on handset devices. In this case you would switch the fragments in the activity whenever necessary. This requires that the fragment is not declared in the layout file as such fragments cannot be removed during runtime.

Use separate activities to host each fragment on a handset. For example, when the tablet UI uses two fragments in an activity, use the same activity for handsets, but supply an alternative layout that includes just one fragment. When you need to switch fragments, start another activity that hosts the other fragment.

The second approach is the most flexible and in general preferable way of using fragments. In this case the main activity checks if the detail fragment is available in the layout. If the detailed fragment is there, the main activity tells the fragment that it should update itself. If the detail fragment is not available, the main activity starts the detailed activity.

这让我开始思考,因为我认为第一个选项会更好。 (我在其他教程的其他地方看到过类似的文字)

使用第一个选项,无论方向/设备类型如何,您都可以始终控制类里面的状态

第二个可能涉及另一项 Activity 。在我看来,这可能有点代码味道,因为这意味着您最终会得到 if/else 语句,这些语句可能会创建一个单独的 Activity 并且逻辑会分散。

我是 android 开发的新手,所以我真的很想听听这里的意见。我错过了什么吗?

谢谢

最佳答案

老实说,这是您的选择。一般来说,当 fragment 出现时,它是一种为平板电脑创建多 Pane 和为手机创建单一 Activity fragment 的方法。惯例已经改变,手机和平板电脑可以使用它们来制作更多的单一职责 View 。只要你遵循 SOLID/Good OO 就应该没问题。坚持使用您为应用选择的样式。

关于android - 一项 Activity 或单独 Activity 中的多个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24985776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com