Cwa-app-android: Simplify Fragments view unbinding code

Created on 25 Jul 2020  路  4Comments  路  Source: corona-warn-app/cwa-app-android

Avoid duplicates

  • [x] This enhancement request has not already been raised before
  • [x] Enhancement request is specific for Android only, for general issues / questions that apply to iOS and Android please raise them in CWA-Wishlist
  • [x] If you are proposing a new feature, please do so in CWA-Wishlist

Current Implementation

The View Binding usage inside the app's Fragments is a bit complicated and repetitive.

  1. duplicated binding properties to be able to get rid of the null checks:
private var _binding: FragmentMainBinding? = null
private val binding: FragmentMainBinding get() = _binding!!
  1. duplicated code, every Fragment override onDestroyView in order to unbind the view:
override fun onDestroyView() {  
     super.onDestroyView()  
    _binding = null 
}

Suggested Enhancement

My suggestion is to create a lifecycle-aware delegated property that automatically unbinds the view during onDestroyView. As a nice side-effect, we would be able to remove the _binding property.

The implementation would be similar to https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719

Expected Benefits

  • The code would become simpler and cleaner.
  • Less duplicated code.
  • Less chance of Fragment memory leaks caused by missing _binding = null statements.

Internal Tracking ID: EXPOSUREAPP-1926

enhancement mirrored-to-jira

Most helpful comment

No problem @svengabr :)
Let me know if you need my help to rebase/update my PR.

All 4 comments

Hi @pwoessner
Any updates from your side regarding this issue/pr?

Hey @henriquenfaria sorry for the late response here. We are currently working hard on finalizing the next release. I have re-assigned the issue to make sure it gets in the hand of the right people soon.

Best regards,
SG

Corona-Warn-App Open Source Team

No problem @svengabr :)
Let me know if you need my help to rebase/update my PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tegutistgut picture tegutistgut  路  3Comments

tandreb picture tandreb  路  3Comments

wmertens picture wmertens  路  3Comments

ironjan picture ironjan  路  3Comments

HuiiBuh picture HuiiBuh  路  3Comments