Is onResume() called before onActivityResult()?

The call to onActivityResult happens before onResume, actually (see the docs). Are you sure you’re actually starting the activity you wanted with startActivityForResult and that you’re setting the result of the invoked activity to RESULT_OK before returning a value to your activity? Try just putting a Log statement in your onActivityResult to log that value … Read more

When is the viewmodel onCleared called

Are ViewModels independent of activity/fragment lifecycles or just their configuration changes. ViewModels (VMs) are independent of configuration changes and are cleared when activity/fragment is destroyed. Following is the lifecycle of ViewModel from official site: Can the viewModel be shared with another Activity ? You shouldn’t do that with Activities. However fragments can share a ViewModel … Read more

How to know when my app has been killed?

I have found one way to do this….. Make one service like this public class OnClearFromRecentService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(“ClearFromRecentService”, “Service Started”); return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.d(“ClearFromRecentService”, “Service Destroyed”); } @Override … 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

IllegalStateException: Fragment already added in the tabhost fragment

This happens when we try to add same fragment or DialogFragment twice before dismissing, just call if(mFragment.isAdded()) { return; //or return false/true, based on where you are calling from } Having said that, I don’t see any reason why to remove old fragment and add the same fragment again since we can update the UI/data … 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

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