activity-lifecycle
Life cycle of Android Activity after pressing Back button
The following activity call back methods are called, after pressing back button. onPause() onStop() onDestroy() The activity is destroyed. And it recreates when launched again. These are the callback methods when it launches again. onCreate() onStart() onResume()
How to simulate killing activity to conserve memory?
You can’t do it in an automated way b/c its completely non deterministic. See my answer here: https://stackoverflow.com/a/15048112/909956 for details. But good news is that all you need to do is just simulate calling onSaveInstanceState and you are indirectly testing this low memory situation. onSaveInstanceState can be triggered by: losing focus (by pressing home which … Read more
Method to override when layout is destroyed in Android
I had success overriding the onAttachedToWindow() and onDetachedFromWindow() methods: @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); // View is now attached } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); // View is now detached, and about to be destroyed }
OnPause and OnStop() called immediately after starting activity
Let us understand why the lifecycle methods are called multiple times. Here is an important code comment documented in ActivityThread, which is responsible for executing the activities of the application process. We accomplish this by going through the normal startup (because activities expect to go through onResume() the first time they run, before their window … Read more
Why implement onDestroy() if it is not guaranteed to be called?
onDestroy will be called if you explicitly call finish(); yourself. Your main activity calls startActivityForResult on a map activity. Map activity with a LocationListener, the user clicks the map and selects say a local restaurant. The activity then , sets up some extras to be sent back to your main activity, it then explicitly call’s … Read more
Activity lifecycle – onCreate called on every re-orientation
android:configChanges=”keyboardHidden|orientation|screenSize” Caution: Beginning with Android 3.2 (API level 13), the “screen size” also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the … Read more
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
What is Activity.finish() method doing exactly?
When calling finish() on an activity, the method onDestroy() is executed. This method can do things like: Dismiss any dialogs the activity was managing. Close any cursors the activity was managing. Close any open search dialog Also, onDestroy() isn’t a destructor. It doesn’t actually destroy the object. It’s just a method that’s called based on … Read more
On logout, clear Activity history stack, preventing “back” button from opening logged-in-only Activities
I can suggest you another approach IMHO more robust. Basically you need to broadcast a logout message to all your Activities needing to stay under a logged-in status. So you can use the sendBroadcast and install a BroadcastReceiver in all your Actvities. Something like this: /** on your logout method:**/ Intent broadcastIntent = new Intent(); … Read more