How We Save Android State at PicsArt

Artyom
Picsart Engineering
3 min readMar 26, 2019

Android development is fun, no doubt about it! Still, there is also a lot of repetitive boilerplate code that the platform forces us to write. Quite a lot of it is related to the UI components that you need to process. Some of it is required when you want your application architecture to be clean. It’s quite easy to end up with a bunch of spaghetti code that is unreadable or just does not feel right.

One common problem for many Android applications is the incorrect handling of the activity and fragment lifecycle. The intended way to save the state of your activity or fragment is to store the contents of all fields in the activity into a bundle, which is then correctly managed by the Android framework through the lifecycle. It can be quite boring to do because you must add all your variables one by one to the bundle that gets saved. Also reading the data from the bundle again, if it even exists, can be challenging.

But what if we can remove all the boilerplate code for saving instance state?

Exactly, that would make our lives way easier!

Annotation processing

If you are writing your application in good old java language the solution is to use annotation processing. There are libraries out there like Icepick which are already doing that.

But what if your application is written in Kotlin? Do you still need that annotation processing? Isn’t there any other solution?

Delegation

The Delegation pattern has proven to be a good alternative to implementation inheritance, and Kotlin supports it natively requiring zero boilerplate code.

Delegation is an implementation mechanism in which an object forwards or delegates a request to another object.

Luckily there is a library Stateful which has already implemented Kotlin delegation for saving instance state. Stateful uses 2 major Kotlin features: delegated properties and delegation. We have created it at PicsArt and shared it with the tech community as open-source code to make everyone’s life easier.

So, as an example let’s say we need to remember the combination of the last searched company and repository.

We delegate to stateful property the field that we want to save to the bundle.

private var lastSearchedCombination: String by statefulProperty("") // This will be automatically saved and restored

The statefulProperty function belongs to the stateful interface. So, does the stateful interface need to be implemented? Not really. What if I say that you can delegate the implementation to an existing one?

class ExampleActivity : Activity(), Stateful by state()

With the help of delegation, there is no difference if you are saving the state of an activity, fragment or something else. Now we need to call save() in the onSaveInstanceState() method:

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
save(outState)
}

And also call restore() in the onCreate() method to restore the state:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
restore(savedInstanceState)
}

Remember, limitations of what you can save to a bundle still exist. However, there’s is no need to mess with adding constants for bundle keys or adding null checks for the savedInstanceState.

The stateful library was created during the development of PicsArt’s video editor. It helped to cut out about 40 lines of code from the video editor and about 380 more lines from the whole app. You may think that it is not much, apps like PicsArt contain thousands of lines of code, but it is boilerplate code that every time you have to read and it is very easy to leave a bug there. Less code, fewer bugs.

Conclusion

If you are writing an application with Kotlin there is no need to extend your build time to process annotations and generate code for saving state. Just use Kotlin language feature and write clean and concise code, let the compiler do its job.

About the author

Artyom Dangizyan is an Android developer at PicsArt. He is passionate about innovation and technology, and he believes that Kotlin will change the way we think about Android software development.

--

--