Maksim Dmitriev Maksim Dmitriev - 2 months ago 10
Android Question

Understanding a retained fragment as a task holder

I just took a look at the two following examples of the fragment as a task holder approach:



Why did they need
UiFragment
in the first example? Messages from the task holder fragment can be sent to its activity directly as it's done in the second example.

Answer

They did not need that Fragment.

It's just a simple example, seems like they chose to use a Fragment for the content instead of calling setContentView() on the Activity.

There are two Fragments basically in the Google example:

  • A Fragment for the UI
  • A worker Fragment to retain the state of a Thread through the re-creation of the Activity (in case of an orientation change for example).

They could have just ditch the UI Fragment and call setContentView() on the Activity and it wouldn't make a difference. The point of the example is the worker Fragment.

Comments