I've created a small program to set the content view on onResume() method instead of onCreate() and its working fine.
onResume()
{
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.ed1);
editText2 = (EditText) findViewById(R.id.ed2);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
Android App Development for Beginners onCreate() is called when the when the activity is first created. onStart() is called when the activity is becoming visible to the user.
onResume() will never be called before onCreate() . Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() . onResume() will also be called the first time, when the activity has been created, right?
onStart() called when the activity is becoming visible to the user. onResume() called when the activity will start interacting with the user. You may want to do different things in this cases.
onCreate is used to start an activity. super is used to call the parent class constructor. setContentView is used to set the xml.
You don't setContentView in onStart or onResume because it's inefficient. Setting up the layout is a pretty heavyweight task. You have to parse the XML, create the Views, measurements, drawing, etc. It's the sort of task that you want to run at most once.
During the lifetime of your Activity class onCreate gets called exactly once. onResume and onStart will get called multiple times without your Activity getting destroyed.
As onCreate()
of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int)
to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String)
to retrieve cursors for data being displayed, etc.
It is inefficient to set the content in onResume()
or onStart()
(which are called multiple times) as the setContentView()
is a heavy operation.
onResume
you may call it serval times. onCreate
is just one time. Try log sth in they and skip to other activity than back.
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