Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle : ON_DESTROY & ON_CREATE are called after config_change even when a retained Fragment is used as LifecycleOwner #127

Closed
viraj49 opened this issue Aug 15, 2017 · 4 comments

Comments

@viraj49
Copy link

viraj49 commented Aug 15, 2017

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,

    compile "android.arch.lifecycle:runtime:1.0.0-alpha5"
    compile "android.arch.lifecycle:extensions:1.0.0-alpha5"
    annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha5"

Activity.kt

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")
    }
}
@JoseAlcerreca
Copy link
Contributor

I can't find an obvious answer but before taking a closer look, have you tried alpha8?

@viraj49
Copy link
Author

viraj49 commented Aug 16, 2017

I just tried with alpha8, unfortunately it's reproducible with alpha8 as well.

    compile "android.arch.lifecycle:runtime:1.0.0-alpha8"
    compile "android.arch.lifecycle:extensions:1.0.0-alpha8"
    annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha8"
E/Activity: OnCreate
E/Fragmemt: OnCreate
E/Observer: onCreate
E/Activity: OnDestroy
E/Observer: onDestroy
E/Activity: OnCreate
E/Observer: onCreate

@pcqpcq
Copy link

pcqpcq commented Sep 8, 2017

I think it's the same issue with #47

recker115 pushed a commit to recker115/architecture-components-samples that referenced this issue Nov 12, 2019
…ion-idling-resource-exception

Modify the exception thrown on decrementing idling resource
@ianhanniballake
Copy link
Contributor

Feel free to file a bug against Lifecycle on the issue tracker if this is still an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants