[英]How to Split Fragment Logic Out of MainActivity.java
I would like to add separate files for each fragment I want to add to my tabbed Android application. 我想为要添加到选项卡式Android应用程序中的每个片段添加单独的文件。 Each tab view should reference code contained in one of those separate files.
每个选项卡视图都应引用这些单独文件之一中包含的代码。
Any input is appreciated as this diverges significantly from what I've read and seems like it could cause a lot of unforeseen problems. 任何输入都会受到赞赏,因为这与我所阅读的内容有很大出入,并且似乎会引起很多无法预料的问题。 Albeit it would be great to have this for readability.
尽管具有可读性非常好。
Not only is this possible, it's good practice. 这不仅可能,而且是一种很好的做法。 Typically, you create separate files for each of your classes.
通常,您为每个类创建单独的文件。 I understand your confusion, though, since the default "Tabbed Activity" project template puts the fragment logic in
MainActivity
. 不过,我理解您的困惑,因为默认的“ Tabbed Activity”项目模板将片段逻辑放在
MainActivity
。 This isn't required. 这不是必需的。
It is completely safe to separate the Fragment-specific logic into its own file. 将特定于片段的逻辑分离到其自己的文件中是完全安全的。
You shouldn't call onCreate
yourself. 您不应该调用
onCreate
自己。 The system does this for you. 系统为您执行此操作。
I modified the "Tabbed Activity" template to use separate files for each tabbed fragment. 我修改了“标签活动”模板,为每个选项卡式片段使用单独的文件。 Check it out here , and let me know if you have any other questions.
在此处查看 ,如果您还有其他问题,请告诉我。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.