AndroidX Part 2: Activity

S M Mohi-Us Sunnat
sunnat629.dev
Published in
3 min readMar 9, 2020

You can read in Bangla blog: অ্যান্ড্রয়েডএক্স পর্ব ২ঃ এক্টিভিটি

Version: 1.2.0-alpha01

Today we will see what’s new in AndroidX activity.

We saw in AndroidX Episode 1: App-Compact that we can now use layout as a parameter of AppCompatActivity as an alternative to setContentView(). Which is a part of this androidx.activity: activity. This is basically for the @ContentView annotation class. This new annotation class has been added into activity#1.0.0-alpha04.

This allows you to use the XML layout in the setContentView() option. If you check AppCompatActivity(R.layout.activity_main), you will find that AppCompatActivity() is an @ContentView annotated constructor.

LifecycleOwner and ViewModelStoreOwner are now implemented from FragmentActivity to ComponentActivity. We’ll see more detail about this in the Lifecycle episode.

In MVVM (Model View ViewModel) architecture, we can now integrate Lifecycle ViewModel SavedState very easily. Now we almost initialize the viewModel like the following code.

But now for the convenience of androidx.activity I can do this very easily. We don’t need ViewModelProvider anymore. Now we can initialize viewModel directly using by viewModels() -

If we notice, here we have used by. That means it’s a lazy instance. This means that this viewModel will be initialized only when it is used. Because of this feature, we can get rid of memory leaks. Someday I’ll write a blog about lazy.

And if we go into viewModels(), we see that it is an Extension function of ComponentActivity whose parameter is Factory, and its default factory is SavedStateViewModelFactory. In viewModel’s blog we will know something about this Factory. by viewModels() is exist in ComponentActivity and its subclass. That means we can also initiate viewModel in Fragment like this.

Lifecycle 2.2.0-rc03 and Lifecycle ViewModel SavedState 1.0.0-rc03, which are dependent on 2 separate libraries for this viewModel’s lifecycle, will be used on Lifecycle’s blog.

In the last version of androidx.activity:activity, there is the last new feature of my blog, OnBackPressedCallback. This is usually a callback to OnBackPressed.

This callback is for handling one or more Fragment’s OnBackPressed events used in Activity.

Let’s look at an example of androidx.drawerlayout.widget.DrawerLayout where we will handle the drawerLayout drawer open-close on onBackPressed()-

Here we are handling logic through if-else in one place, but we can easily handle DrawerLayout with OnBackPressedCallback.
Now we can use OnBackPressedCallback to dismiss the navigation view, and register the activity for it on onBackPressedDispatcher.This conditional logic, callback enable or disable can be handled.

In the example above we can see that we are initializing a callback which is an object of OnBackPressedCallback. OnBackPressedCallback has an abstract class handleOnBackPressed, where we can do the work of onBackPressed().

This was just a simple example but during using multiple Fragments can be easily controlled through OnBackPressedCallback.

So far today. To see the change of activity: link

We will look at androidx.fragment later in the blog.

If you find this blog to be of benefit to you, give me a clap to support and follow the channel for upcoming blogs.

Happy coding…

--

--