When an activity is created for the first time then system calls the OnContentChanged()
method as the first method and last call by system is the OnDetachedFromWindow()
method when an activity is killed, but android docs says entire lifetime of an Activity happens between OnCreate()
and OnDestroy()
. Why? Please help me in understanding difference between these methods.
Code:
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;
public class ActivitylifecycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onContentChanged() {
super.onContentChanged();
Toast.makeText(getApplicationContext(),"1. onContentChanged()", Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getApplicationContext(),"2. onCreate()", Toast.LENGTH_SHORT).show();
}
@Override
public void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"3. onStart()", Toast.LENGTH_SHORT).show();
}
@Override
public void onRestoreInstanceState(Bundle restoreInstanceState) {
Toast.makeText(getApplicationContext(),"4. onRestoreinstaneState()", Toast.LENGTH_SHORT).show();
super.onRestoreInstanceState(restoreInstanceState);
}
@Override
public void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(),"5. onRestart()", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostCreate(Bundle onpostcrete) {
super.onPostCreate(onpostcrete);
Toast.makeText(getApplicationContext(),"6. onPostCreate()", Toast.LENGTH_SHORT).show();
}
@Override
public void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"7. onResume()", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostResume() {
super.onPostResume();
Toast.makeText(getApplicationContext(),"8. onPostResume()", Toast.LENGTH_SHORT).show();
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Toast.makeText(getApplicationContext(),"9. onAttachedToWindow()", Toast.LENGTH_SHORT).show();
}
@Override
public void onWindowFocusChanged(boolean bo) {
super.onWindowFocusChanged(true);
Toast.makeText(getApplicationContext(),"10. onWindowFocusChanged()", Toast.LENGTH_SHORT).show();
}
@Override
public void onUserLeaveHint() {
super.onUserLeaveHint();
Toast.makeText(getApplicationContext(),"11. onUserLeaveHint()", Toast.LENGTH_SHORT).show();
}
@Override
public void onUserInteraction() {
super.onUserInteraction();
ii=0;
Toast.makeText(getApplicationContext(),"12. onUserInteraction()", Toast.LENGTH_SHORT).show();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Toast.makeText(getApplicationContext(),"13. onSaveInstanceState()", Toast.LENGTH_SHORT).show();
}
@Override
public void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(),"14. onPause()", Toast.LENGTH_SHORT).show();
}
@Override
public void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"15. onStop()", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"16. onDestroy()", Toast.LENGTH_SHORT).show();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
Toast.makeText(getApplicationContext(),"17. onDetachedFromWindow()", Toast.LENGTH_SHORT).show();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(getApplicationContext(),"18. onConfigurationChanged()", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSearchRequested() {
super.onSearchRequested();
Toast.makeText(getApplicationContext(),"19. onSearchRequested()", Toast.LENGTH_SHORT).show();
return false;
}
}
In this code, onContentChanged()
is called before onCreate()
method and onDetachedFromWindow()
is called after onDestroy()
. Why?
In the Android Activity Lifecycle's onDestroy docs: onDestroy() is called before the activity is destroyed.
Difference between onPause(), onStop() and onDestroy() So, onPause() is logically before onStop(). From onPause() it is possible to call onResume() but it is not possible once onStop() is called. Once onStop() is called then onRestart() can be called. onDestroy() is last in the order after onStop().
As logs demonstrated, the first instance is wiped out, but not until after the second instance has been fully created and has window focus. Thus, I was able to see onDestroy() called on my started activity, after onCreate().
onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.
onCreate():
When an activity starts its life onCreate()
is called. It is called only once in the lifecycle of an activity.
onDestroy():
onDestroy()
is called when an activity finishes its life cycle. It is also called once in the lifecycle of an activity.
onContentChanged():
This hook is called whenever the content view of the screen changes (due to a call to Window.setContentView
or Window.addContentView
). For example you add new view to activity or want to refresh the list by calling notifyDataSetChanged()
.
onDetachedFromWindow():
Called when the main window associated with the activity has been detached from the window manager. For example, it is called when the current Activity goes into background or another activity came infront of current activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With