Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android non-UI Fragment usage [duplicate]

Tags:

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.

According to the documentation

you can also use a fragment to provide a background behavior for the activity without presenting additional 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.

like image 612
anna Avatar asked Jan 24 '14 14:01

anna


People also ask

Can we use fragment without UI?

The Android Developer guide has a decent section on the use of Fragments. One way to use Fragments is without a UI. There are a few references to using this as a means of background processing, but what advantages do Fragments bring to this area?

How do you stop a fragment from recreating?

Calling setRetainInstance(true) will prevent the Fragment from being re-created.

Are fragments reusable?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.

What does the onCreateView () method return if a fragment doesn't have any UI?

These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.


1 Answers

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.

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):

https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java

like image 76
marcinj Avatar answered Nov 02 '22 23:11

marcinj