You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I came across this unexpected behaviour when using AAC-Lifecycle alpha5, where I am using a retained fragment as a LifecycleOwner and a regular class as LifecycleObserver and upon configuration change (orientation change), my Fragment's onDestroy & onCreate were not called as expected, but my Observer class's onDestroy & onCreate methods were called, which was strange for me.
Is this a bug with AAC-Lifecycle or issue with my implementation or am I missing something here?
class Activity : LifecycleActivity() {
private val TAG = "Activity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity)
Log.e(TAG, "OnCreate")
if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
.replace(R.id.frame_layout, Fragment())
.commit()
}
}
override fun onDestroy() {
Log.e(TAG, "OnDestroy")
super.onDestroy()
}
}
Fragment.kt
class Fragment : LifecycleFragment() {
private val TAG = "Fragmemt"
private val observer = Observer()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
retainInstance = true
lifecycle.addObserver(observer)
Log.e(TAG, "OnCreate")
}
override fun onCreateView(inflater: LayoutInflater?,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater?.inflate(R.layout.fragment, container, false)
}
override fun onDestroy() {
Log.e(TAG, "OnDestroy")
lifecycle.removeObserver(observer)
super.onDestroy()
}
}
Observer.kt
class Observer : LifecycleObserver {
private val TAG = "Observer"
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
Log.e(TAG, "onCreate")
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
Log.e(TAG, "onDestroy")
}
}
The text was updated successfully, but these errors were encountered:
I came across this unexpected behaviour when using AAC-Lifecycle alpha5, where I am using a retained fragment as a LifecycleOwner and a regular class as LifecycleObserver and upon configuration change (orientation change), my Fragment's onDestroy & onCreate were not called as expected, but my Observer class's onDestroy & onCreate methods were called, which was strange for me.
Is this a bug with AAC-Lifecycle or issue with my implementation or am I missing something here?
Here is the code I used,
Activity.kt
Fragment.kt
Observer.kt
The text was updated successfully, but these errors were encountered: