Android的非UI片段使用 [英] Android non-UI Fragment usage

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

问题描述

我会通过的Andr​​oid文档的片段的。它定义了一个片段的UI上布置可以在活动的布局来限定,在一个单独的.xml文件或根本没有。

I am going through the Android documentation on Fragments . The layout that defines the UI of a Fragment may be defined in the layout of the Activity, in a separate .xml file or not at all.

根据文档

您也可以使用一个片段,为活动后台行为,而presenting额外的用户界面。

you can also use a fragment to provide a background behavior for the activity without presenting additional UI.

为什么我需要使用定义的活动中多了一些功能的另一个片段将功能添加到一个活动呢?请问这样的非UI片段中使用,只是为了模块化的?难道还有其他原因,采取这种做法?我将AP preciate当合适的是使用非UI片段的一个例子。

Why would I need to use another Fragment to add functionality to an Activity instead of defining a few more functions within the Activity? Would such a non-UI Fragment be used just for the sake of modularity? Is there another reason for adopting such an approach? I would appreciate an example of when it is suitable to use a non-UI fragment.

感谢你提前为您的帮助。

Thank you in advance for your assistance.

推荐答案

我想这是对保留的片段,里面的片段可以调用setRetainedInstance(真),这样一来你的片段将不会在配置变化来重新创建。通常情况下,当你旋转你的设备,所有片段将被重新创建。如果调用setRetainedInstance(真)内的onCreate(),那么你的片段实例将不会被重建。

I suppose this is about retained fragments, Inside fragment you can call setRetainedInstance(true), this way your fragment will not be recreated during configuration changes. Normally when you rotate your device, all fragments will be recreated. If you call setRetainedInstance(true) inside onCreate(), then your fragment instance will not be recreated.

请告诉我利用呢? - 你可以把一些数据,你的片段里面阵列等,它不会在配置更改被破坏。你也可以把异步任务,这样的片段里面,后主要活动旋转,在片段的异步任务仍然能够提供它的结果。

Whats use of it? - you can put some data, arrays etc. inside your fragment and it will not be destroyed during configuration changes. You can also put async task inside such fragment, and after main activity rotates, your async task in your fragment will still be able to deliver its results.

片段的另一个有用的特点是,你可以轻松地重用他们在多个活动。这意味着你可以把一些常用的逻辑,你的非UI片段里面。你可以让说与你的活动的基类完成它,但你只能扩展一个类。

Another usefull feature of fragments is that you can easily reuse them in multiple activities. This means you can put some common logic inside your non UI fragment. You could lets say accomplish it with base class for your activity, but you can extend only one class.

和从谷歌(实际使用线程内保留的片段)简单的例子:

and simple example from google (actually using Thread inside retained fragment):

<一个href="https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java" rel="nofollow">https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java

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

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