avatarElye - A One Eye Dev By His Grace

Summary

The web content provides guidance on how to disable automatic fragment restoration in Android applications for developers who prefer full control over the app's state.

Abstract

The article discusses the common issue faced by Android developers when the system automatically restores fragments, leading to unexpected behavior. It explains the typical scenario where developers might encounter this issue, particularly when the system reinstates a fragment after the app has been killed. The author suggests a solution to prevent automatic restoration by passing null to super.onCreate instead of the savedInstanceState bundle. This approach ensures that the app always starts fresh without any state restoration, which is suitable for simple apps where preserving state is not critical. The article also includes a disclaimer that this method is not recommended for professional app development, as it bypasses the standard state restoration process, which is essential for a good user experience in more complex applications.

Opinions

  • The author acknowledges the complexity of handling fragment state restoration and views it as an unnecessary hassle for simple apps.
  • The author's opinion is that developers should have the option to bypass the default behavior for specific cases where full manual control is desired.
  • The author provides a disclaimer, emphasizing that the suggested approach is not ideal for professional app development and should be used with caution and understanding of the implications.
  • The article implies that the standard fragment restoration process is beneficial for more complex applications and should be adhered to in a professional context.

Manually override Fragments Auto Restoration

Fragment is the way to go for Android Development. However, for starter that learn Android Development, many might plagued with strange Fragment behavior that happens when the system restore the App.

Code as below definitely will have demonstrate strange behavior.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container, MainFragment())
            .commit()
    }
}

For those experience in Android Development, we know the logic behind. It is simply because whenever the System Kills the App (that has Fragment in place), it will put the Fragment back in place automatically.

So with that, we should code in such a way, to create a new fragment IF AND ONLY IF when the App is NOT restored by the system (like code below), since the system will restore the fragment automatically.

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (savedinstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.container, MainFragment())
                .commit()
        }
    }
}

The above is the simple part. Other than that, we also need to code within the Fragment itself too to handle restoration, and also if we have data model we need to saved and restore it etc.

So much to do 😓… can I skip that 🧐 ?

It is just too much to do if we’re writing a simple App that we don’t mind not restoring the Fragment by the system.

What we want is to whenever the system restore the App, just like starting the new App afresh. We don’t need to restore the state… just simple App that always restart if user terminate it, or when the system terminate it. No need state restoration!

Yes you can skip that 😊

Good news. That is possible, and very simple.

It just simply override the super.onCreate by passing in null instead of savedInstanceState.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(null)
        setContentView(R.layout.activity_main)
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.container, MainFragment())
            .commit()
    }
}

That’s it.

With this, you need not worry about State Restoration, and the killed Fragment will not be restored automatically.

Disclaimer: I’m not advocating to use this for professional app development. This is not ideal, and only useful for specific case (where you want to have full control of the entire restoration manually) as well as a really simple App where restoration is not needed.

I hope this post is helpful to you. You could check out my other interesting topics here.

Follow me on medium, Twitter or Facebook for little tips and learning on Android, Kotlin etc related topics. ~Elye~

Android
Android App Development
AndroidDev
Mobile App Development
Google Developer Group
Recommended from ReadMedium