Jens Buysse
Jens Buysse

Reputation: 139

Best Practise dual-pane in Android using only 1 activity and two fragments in Android

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.

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

Upvotes: 1

Views: 624

Answers (1)

SubChord
SubChord

Reputation: 1454

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

Upvotes: 1

Related Questions