Fixed
Status Update
Comments
si...@google.com <si...@google.com>
mo...@gmail.com <mo...@gmail.com> #2
+1, this is a severe bug if I've ever seen one. If it is on par with the #74139250 (https://issuetracker.google.com/issues/74139250 ), this should be at least P1
si...@google.com <si...@google.com> #3
1) Thank you for reporting this issue.We were not able to reproduce the issue with the information provided here.
Logs after follow mentioned steps in comment #1
===============================================
V/TAG: ViewModelFirst Created
2) Can you please provide the below requested information to better understand the issue:
Android build
Which Android build are you using? (e.g. KVT49L)
Device used
Which device did you use to reproduce this issue?
Please provide sample project or apk to reproduce the issue. Also mention the steps to be followed for reproducing the issue with the given sample project or apk.
Logs after follow mentioned steps in
===============================================
V/TAG: ViewModelFirst Created
2) Can you please provide the below requested information to better understand the issue:
Android build
Which Android build are you using? (e.g. KVT49L)
Device used
Which device did you use to reproduce this issue?
Please provide sample project or apk to reproduce the issue. Also mention the steps to be followed for reproducing the issue with the given sample project or apk.
lb...@gmail.com <lb...@gmail.com> #4
al...@gmail.com <al...@gmail.com> #5
@3
I have this problem on all my devices. Here are some of them:
1. Samsung Galaxy S9 (SM-G960F)
Android: 8.0.0 build: R16NW.G960FXXU1BRE5
2. Pixel2
Android: 9 build: PPP3.180510.008
3. Galaxy A3 (SM-A320FL)
Android: 7.0 build: NRD90M.A320FLXXS2BRAA
The three attachments included are: The built apk, the source code and a video displaying the issue.
The video showcases the app running on Galaxy S9 (Phone 1) with the following order:
1. Set "Don't keep activities" to enabled.
2. Happy case when everything is correct and onCleared() is called.
3. Fault case when the home button is pushed and onCleared() __is not called__.
I have this problem on all my devices. Here are some of them:
1. Samsung Galaxy S9 (SM-G960F)
Android: 8.0.0 build: R16NW.G960FXXU1BRE5
2. Pixel2
Android: 9 build: PPP3.180510.008
3. Galaxy A3 (SM-A320FL)
Android: 7.0 build: NRD90M.A320FLXXS2BRAA
The three attachments included are: The built apk, the source code and a video displaying the issue.
The video showcases the app running on Galaxy S9 (Phone 1) with the following order:
1. Set "Don't keep activities" to enabled.
2. Happy case when everything is correct and onCleared() is called.
3. Fault case when the home button is pushed and onCleared() __is not called__.
il...@google.com <il...@google.com> #6
I was able to reproduce this with "Don't keep activities" on.
Fragments currently use the fact that the system called onSaveInstanceState() as a marker on whether the Activity could be recreated. "Don't keep activities" specifically breaks that assumption by saving the instance state, then immediately throwing it away.
In a typical system, the only time that saved instance state would be thrown away would be if your process is being destroyed, of which there is no callback (either to your app or to ViewModels). Therefore this bug is limited to only devices using "Don't keep activities".
Saved state is an imperfect signal here, so we'll switch to the correct signal - Activity.isChangingConfigurations().
Fragments currently use the fact that the system called onSaveInstanceState() as a marker on whether the Activity could be recreated. "Don't keep activities" specifically breaks that assumption by saving the instance state, then immediately throwing it away.
In a typical system, the only time that saved instance state would be thrown away would be if your process is being destroyed, of which there is no callback (either to your app or to ViewModels). Therefore this bug is limited to only devices using "Don't keep activities".
Saved state is an imperfect signal here, so we'll switch to the correct signal - Activity.isChangingConfigurations().
lb...@gmail.com <lb...@gmail.com> #7
@6 Please also try my sample. on my sample I didn't enable "don't keep activities".
il...@google.com <il...@google.com> #8
The issue with onCleared not being called on Fragment hosted ViewModels is fixed internally and will be available in future releases of fragments.
[Deleted User] <[Deleted User]> #9
@8 What release will that be? Will it be part of the support library?
il...@google.com <il...@google.com> #10
Every release of the 28.0.0 Fragments and 1.0.0 AndroidX Fragments released after June 29th contains this fix, including beta01 and rc01. If you're still having issues, please file a new bug with reproduction steps and a sample project.
lb...@gmail.com <lb...@gmail.com> #11
@10 Have you tested it on the issue I've presented too:
https://issuetracker.google.com/issues/76404690
?
Can you reproduce it there? If you can, it shouldn't be closed. If you can't maybe it should be closed too.
?
Can you reproduce it there? If you can, it shouldn't be closed. If you can't maybe it should be closed too.
lb...@gmail.com <lb...@gmail.com> #12
@10 OK seems it's also fixed. You should close it too.
Description
com.android.support:appcompat-v7
Version used:
28.0.0-alpha3, 28.0.0-alpha1, 27.1.1
- Relevant code to trigger the issue:
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")
}
fun onStartClicked(view: View) {
supportFragmentManager
.beginTransaction()
.replace(R.id.container, FragmentSecond())
.addToBackStack("second")
.commit()
}
}
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")
}
}
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")
}
}
The test project is here:
Steps to reproduce:
1. Have Don't keep activities enabled.
2. Start the app.
3. Push home button.
Expected behaviour is the following output at logcat:
V/ViewModelFirst: Created
V/ViewModelFirst: onCleared
V/FragmentFirst: onDestroy
V/MainActivity: onDestroy
This is how it exactly works on 27.0.2.
Real behaviour is the following output at logcat:
V/ViewModelFirst: Created
V/FragmentFirst: onDestroy
V/MainActivity: onDestroy
This is how it works on all the released versions of support library starting from 27.1.1 and till 28.0.0-alpha3
As we can see activity and fragment was destroyed but viewModel was not notified with onCleared.
I suspect that if in case the "Don't keep activities" is disabled and the app at background is being unloaded by Android at some moment of time "viewModel.onCleared()" will not be called which is incorrect behaviour.