I stumbled on a problem when ViewModel.OnCleared()
is not being called when the app goes to background (even if Don't keep activities
is enabled) but I can see that Fragment.onDestroy()
is actually being called.
What could be wrong in the following code? How can I make ViewModel.OnCleared()
to be actually called in this scenario?
ViewModel:
class ViewModelFirst(application: Application) : AndroidViewModel(application) {
companion object {
private const val TAG = "ViewModelFirst"
}
init {
Log.v(TAG, "Created")
}
override fun onCleared() {
super.onCleared()
Log.v(TAG, "onCleared")
}
}
Fragment:
class FragmentFirst : Fragment() {
companion object {
private const val TAG = "FragmentFirst"
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
ViewModelProviders.of(this).get(ViewModelFirst::class.java)
return inflater.inflate(R.layout.fragment_first, container, false)
}
override fun onDestroy() {
super.onDestroy()
Log.v(TAG, "onDestroy")
}
}
Activity:
class MainActivity : AppCompatActivity() {
companion object {
private const val TAG = "MainActivity"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction().replace(R.id.container, FragmentFirst()).commit()
}
}
override fun onDestroy() {
super.onDestroy()
Log.v(TAG, "onDestroy")
}
}
Answer myself:
It's a bug of com.android.support:appcompat-v7:27.1.0
I am experiencing this issue if I use the following dependencies:
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation "android.arch.lifecycle:extensions:1.1.0"
If I change the version of appcompat-v7
27.1.0 -> 27.0.2, then ViewModel.OnCleared()
works as expected (I have the call when the app goes to background).
appcompat-v7:28.0.0-alpha1
works as well, looks like this is a problem only of appcompat-v7:27.1.0
Update (June 2018)
As @Akshay said, the bug was kinda fixed on 27.1.1. But not fully unfortunately.
The following scenario is still unfixed:
Don't keep activities
enabled. On 27.0.2 I have the following output at logcat:
V/ViewModelFirst: Created
V/ViewModelFirst: onCleared
V/FragmentFirst: onDestroy
V/MainActivity: onDestroy
Which totally correct.
But on 27.1.1 till 28.0.0-alpha3 I have the following output at logcat:
V/ViewModelFirst: Created
V/FragmentFirst: onDestroy
V/MainActivity: onDestroy
As we can see activity and fragment was destroyed but viewModel was not notified with onCleared.
I suspect that in case if the Don't keep activities
will be disabled and the app at background will be naturally unloaded by Android (due another app claiming for a bunch of resources) at some moment of time the viewModel.onCleared()
will not be called which is very sad.
P.S. I have pushed the code here: https://github.com/allco/onClearedInvestigation
And have reported the issue to Google here: https://issuetracker.google.com/issues/110285295
Update (August 2018)
28.0.0-rc01 has this problem solved. Yay!
It was an issue in Support Library v27.1.0 and has been fixed in Support Library v27.1.1.
There is a bug reported previously at https://issuetracker.google.com/issues/74139250.
Refer this link for more details: https://developer.android.com/topic/libraries/support-library/revisions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With