Jetpack compose – how do I refresh a screen when app returns to foreground

I came up with this: @Composable fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) -> Unit) { val eventHandler = rememberUpdatedState(onEvent) val lifecycleOwner = rememberUpdatedState(LocalLifecycleOwner.current) DisposableEffect(lifecycleOwner.value) { val lifecycle = lifecycleOwner.value.lifecycle val observer = LifecycleEventObserver { owner, event -> eventHandler.value(owner, event) } lifecycle.addObserver(observer) onDispose { lifecycle.removeObserver(observer) } } } It seems to work just fine. But there … Read more

Activity.finish() called but activity stays loaded in memory

You don’t control when your app leaves main memory, the OS does. Look closely at Activity.finish… Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult(). Note that is says nothing about memory. As to calling Activity.onResume, that’s exactly what you would expect … Read more

When exactly are onSaveInstanceState() and onRestoreInstanceState() called?

Per the documentation: void onRestoreInstanceState (Bundle savedInstanceState) This method is called between onStart() and onPostCreate(Bundle). void onSaveInstanceState (Bundle outState) If called, this method will occur after onStop() for applications targeting platforms starting with Build.VERSION_CODES.P. For applications targeting earlier platform versions this method will occur before onStop() and there are no guarantees about whether it will … Read more

What is the correct order of calling superclass methods in onPause, onStop and onDestroy methods? and Why?

Destroying the instance specific resources first, before destroying superclass resources that the instance specific resources may depend upon makes sense, not the other way round. But the comments suggest otherwise. What am I missing? In my opinion: not a single thing. This answer from Mark (aka CommonsWare on SO) sheds light on the issue: Link … Read more

Android – Preserve or delete files created by the application on uninstall

Seems like there have been some developments since 2009 :). From the documentation: If you’re using API Level 8 or greater, use getExternalCacheDir() to open a File that represents the external storage directory where you should save cache files. If the user uninstalls your application, these files will be automatically deleted. However, during the life … Read more

What is the benefit of using Fragments in Android, rather than Views?

The main reason to use Fragments are for the backstack and lifecycle features. Otherwise, custom views are more light weight and simpler to implement. At first, I actually tried to build a phone/tablet app using custom views. Everything appeared to work across phones AND tablets, even switching from single panel to split panel. Where I … Read more

fragment lifecycle: when “ondestroy” and “ondestroyview” are not called?

Take a look at this question: Why implement onDestroy() if it is not guaranteed to be called? Basically, onDestroy() is only guaranteed to be called if you call finish(). Otherwise, onDestroy() may not be called until the system deems it necessary. You might want to look at putting your “closing” logic in the onPause() or … Read more

One Activity and all other Fragments [closed]

It depends on the app you are creating. I’ve created several apps using both approaches and can’t say one way is always better than the other. The latest app I created I used the single Activity approach and a Facebook style navigation. When selecting items from the navigation list I update a single Fragment container … Read more