简体   繁体   English

Android Fragments - 多项活动还是一项?

[英]Android Fragments - multiple activities or one?

I'm a new android "developer" and have a question about "Android Fragment - one vs. multiple activities". 我是一个新的Android“开发者”,并有一个关于“Android片段 - 一个与多个活动”的问题。

While there are 2-3 questions on this topic, they are already a few years old. 虽然这个主题有2-3个问题,但它们已经有几年了。

For training, I wanted to create a simple app with an action bar and multiple fragments and i want to use the camera. 对于培训,我想创建一个带有操作栏和多个片段的简单应用程序,我想使用相机。

What is the best way to create something like this? 创建这样的东西的最佳方法是什么? One activity which manage all the fragments or multiple activities? 管理所有片段或多个活动的一项活动? Since i want to use the camera, it is probably easier to use multiple activities (camera intent etc...) - but i don't know - i'm new to android ;) 由于我想使用相机,它可能更容易使用多个活动(相机意图等...) - 但我不知道 - 我是Android的新手;)

Are there any benefits to use more activities instead of one? 使用更多活动而不是一个活动有什么好处吗?

I always prefer the following setup: 我总是喜欢以下设置:

1. A own Application - Class. 1.自己的应用程序 - 类。
This gives you the availability to use and implement common patterns such as registry, eg 这使您可以使用和实现常见模式,例如注册表
The application object can be retrieved in every activity via the getApplicationContext 可以通过getApplicationContext在每个活动中检索应用程序对象
-method and is singleton per nature. - 方法,每个性质是单身。 Also it's the only object that is guaranteed to 它也是唯一可以保证的对象
be existing through your application's lifecycle. 通过应用程序的生命周期存在。 This makes it a most suitable entry point for all sort of implementation details according to objects that need to be existing trough every part of your application. 这使得它成为所有类型的实现细节的最合适的入口点,根据需要存在于应用程序的每个部分的对象。

2. Following 1. , I register both my Activity - objects and Fragment 's in this 2.在1.之后,我在这里注册了我的Activity - objects和Fragment
application class. 应用程序类。 This allows additional cross - activity and cross - fragment 这允许额外的交叉活动和交叉片段
implementations. 实现。

I know that this approach seems to be rather monolithic and I wouldn't overuse concepts 我知道这种方法似乎是相当单一的,我不会过度使用概念
like this, but it is handy in many cases, may it be reading additional application 像这样,但在很多情况下它很方便,可能是在阅读其他应用程序
configuration resources from the asset - directory or establishing database connections so and reuse them. 来自资产的配置资源 - 目录或建立数据库连接,以便重用它们。
So that's where I instanziate my fragments and this kind of setup gives me full 这就是我实现片段的地方,这种设置让我充实
flexibility and control over my application. 灵活性和控制我的应用程序。 Because of the reusability of fragments I 因为片段I的可重用性
tend to use them, and: Google strongly recommend the use of fragments. 倾向于使用它们,并且: Google强烈建议使用碎片。 They did quiet a lot of heavy low - level optimizations on rendering components at system / os 他们确实在系统/操作系统上渲染组件时进行了大量的低级优化
- layer and I tend to trust them. - 层和我倾向于信任他们。

I think, one FragmentActivity holding other fragments will be good. 我认为,一个FragmentActivity持有其他片段会很好。 I already have done a project, firing camera intents from fragments. 我已经完成了一个项目,从片段中发射相机意图。 You can fire intents (for result) and manage the fragment replacing in your FragmentActivity onActivityResult() function. 您可以触发意图(结果)并管理FragmentActivity onActivityResult()函数中的片段替换。

firing camera fragment. 发射相机片段。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE);

//You fragment Activity.. When camera intent ends. //片段活动..当摄像机意图结束时。 onActivityResult() will be called. 将调用onActivityResult()。

public void onActivityResult(.......)
{
//check is it returning from camera, do whatever you want to do.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM