1

Introduction

Setting up a two pane application with two Activities and two possible fragments is discussed in several tutorials, see e.g. the tutorial from Lance Gleason. ALso the stubb code from Android Studio uses the kind of implementation.

The problem

The problem is that all tutorials only cover the static implementation method (using Fragments in XML) or use another activity, mainly to display the details in two pane mode.

Thing is, Andoroid states that a single activity application is the way to go from now on: Today we are introducing the Navigation component as a framework for structuring your in-app UI, with a focus on making a single-Activity app the preferred architecture.

The problem with the single activity solution is that if you want to save the state of your Fragment during a configration change, things become difficult.

  • Although the Activity is rebooted, the Fragment is not
  • Reusing the already created fragments in the Activity (which has rotated) and managing the Fragments becomes tedious and difficult to maintain. See e.g. the solution provided by Danielle B

Question

What is the best practices solution, for a dual pane architecture in Android, using only 1 Activity, two fragments, while maintaining the state of the Fragments during a configuration change?

Kind regards

Jens Buysse

Community
  • 1
  • 1
Jens Buysse
  • 119
  • 10

1 Answers1

1

When using architecture components (AC) it's common practice to make use of the ViewModel class. This class makes communication between fragments possible and maintains its state when configuration changes take place.

Have a look at the docs describing the AC ViewModel: https://developer.android.com/topic/libraries/architecture/viewmodel

SubChord
  • 1,444
  • 1
  • 10
  • 14
  • This looks promising, but is there an example which shows the complete interaction between the Fragments, the single Activity and the ViewModel. Moreover, is using the ViewModel the only way to tackle the problem? – Jens Buysse Oct 15 '18 at 14:28