片段作为静态类 [英] Fragments as static classes

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

问题描述

只是一个简单的问题:在所有我见过的android的文档中的例子,片段是静态内部类。那是Android系统的要求?或者他们是否被设置为常规内部类?有没有人在那里谁理解了Android的内部足以提供一个答案?

这是我看过的OCJP文档中,这些静态内部类不应该出现在类的所有,但它们所包含的类的只是静态成员,就像任何静态方法 - 例如主

您的见解是AP preciated。

解决方案
  

时的机器人这一要求?或者他们是否被设置为常规内部类?

它们不能是常规(非静态)内部类。只有在外部类的一个实例可以创建一个普通的内部类的一个实例,而Android需要重新​​创建片段为你(例如,在配置更改)。片段都必须是普通的Java类或静态内部类,他们需要有一个公共的零参数构造函数。

  

这些静态内部类不应该出现在类的所有,但类,其中包含它们只是静态成员,就像任何静态方法 - 如主

我不知道你是如何来到国米pretation。

Just a brief question: In all the examples I've seen in the android documentation, fragments are static inner classes. Is that a requirement of Android? Or can they be set up as regular inner classes? Is there someone out there who understands the internals of Android enough to provide an answer?

From what I've read in the OCJP documentation, these static inner classes are not suppose to be classes at all, but are just static members of the class in which they are contained, just like any static method - such as main.

Your insights are appreciated.

解决方案

Is that a requirement of Android? Or can they be set up as regular inner classes?

They cannot be regular (non-static) inner classes. Only an a instance of the outer class can create an instance of a regular inner class, and Android needs to recreate your fragments for you (e.g., on a configuration change). Fragments have to be either regular Java classes or static inner classes, and they need to have a public zero-argument constructor.

these static inner classes are not suppose to be classes at all, but are just static members of the class in which they are contained, just like any static method - such as main.

I have no idea how you came to that interpretation.

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

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