Search code examples
androidfirebase-realtime-databaseactivity-lifecycle

Best Place to get Firebase References in Android app


Hello I am developing an app with firebase backend and I need to know how to improve performance in android because I am taking firebase instance and reference to some data in activity or fragment where I will to put its data, but if user rotates your device this activity or fragment is destroyed and when its is recreated is necessary takes firebase references again and pass it to recycler adapter and this may take unecessary process. I need to put firebase references in another place where it don't be lost when rotation or another situation happens.


Solution

  • There are a lot of options to keep data on configuration change just to mention a few:

    • Create a headless fragment which is not getting destroyed on config change (this is the suggested way by google)
    • Store your object as static field

    some reading material:

    http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html

    https://developer.android.com/guide/topics/resources/runtime-changes.html