Issue
I'm just beginning to learn Android Studio. According to Android developer all activity visible to the user happens between onStart and onStop. How come the text "onCreate" written in my simple program, displays on first run?
public class MainActivity extends AppCompatActivity {
private TextView texten = null;
private void print(String text) {
String line = text + "\n";
texten.setText(texten.getText() + line);
}
DecimalFormat dec = new DecimalFormat("0.0000");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
texten = new TextView(this);
texten.setText("");
setContentView(texten);
print("onCreate");
}
...
Solution
If you see the Activity life cycle, first method to be called on activity creation is onCreate()
. The life cycle has been divided into three parts as below.
The entire lifetime of an activity happens between the first call to
onCreate(Bundle)
through to a single final call toonDestroy()
. An activity will do all setup of "global" state inonCreate()
, and release all remaining resources inonDestroy()
. For example, if it has a thread running in the background to download data from the network, it may create that thread inonCreate()
and then stop the thread inonDestroy()
.The visible lifetime of an activity happens between a call to
onStart()
until a corresponding call toonStop()
. During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register aBroadcastReceiver
inonStart()
to monitor for changes that impact your UI, and unregister it inonStop()
when the user no longer sees what you are displaying. TheonStart()
andonStop()
methods can be called multiple times, as the activity becomes visible and hidden to the user.The foreground lifetime of an activity happens between a call to
onResume()
until a corresponding call toonPause()
. During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.
If you see the description of onCreate
it say-
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one. Always followed by onStart().
onCreate
is place we setup our views. Once this is done, next callback is onStart
, on call of which you see the view create in onCreate
.
So, here is your answer: You are creating the view in onCreate
which is displayed after the execution of onStart
. It's not like that whatever you do in onCreate
has no visible effect. You are setting the text, which means creating the view and in turn it is displayed once activity is visible.
Answered By - Rohit5k2
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.