when application installed and opend first time,and i pressed home key then application minimises normally.but when i starts same application(not minimised one),it shows me blank screen.this happens only first time.i am not getting what happens..can any body help
When you starts a fresh application, it first call onCreate then onStart then onResume
When you press home key it will call onPause then onStop
So when you start it again (by clicking its icon) it call onRestart then onStart then onResume, will not call onCreate here
So if you feel some different behavior, then check the code in any of these function if you Override them
Refer this link also http://developer.android.com/reference/android/app/Activity.html
Related
I am totally new to Android and I'm just learning about the Activity life cycle.
In all the apps that I have made so far to practice, I hadn't been using the onStart() method (mostly because I didn't know of it) and the apps worked perfectly fine.
Why though did they work perfectly fine?
And when do I have to explicitly write an onStart() method in my app then?
That´s because your activities are subclasses from Activity or AppCompatActivity. You don´t need to override this method to make the activity work. If you want to know when to use that method you can check this post:
android: when to use onStart(), onStop()?
On create
Is called when the activity is created and then never called again. Unless you open the activity again.
On start
Is called when the activity is created and also called again every time the activity is resumed (if you return to it using back button).
example
lets say we want to show a toast message we will call it "message".
First case
if we want to show "message" only when we create the activity we add the toast in Oncreate, and this is what happens
If you open activity A ----> Oncreate will be called -----> "message" is shown-----> Onstart called -----> nothing happens
If you open from A another activity B and press back ----> onCreate is ignored -----> onStart is triggered -----> nothing happens.
((SO MESSAGE IS SHOWN ONLY ONCE WHEN YOU CREATE THE ACTIVITY)).
Second case
If we want to show "message" each time activity is shown or everytime it becomes visible, we add the toast in onStart, and this what happens:
If you open activity A ----> Oncreate will be called -----> nothing happens-----> Onstart called -----> "message" shown
If you open from A another activity B and press back ----> onCreate is ignored -----> onStart is triggered -----> "message" shown again.
((SO HERE MESSAGE IS SHOWN WHEN WE CREATE ACTIVITY AND WHEN WE GO BACK TO IT)).
This is why on start is not always important to use for app to function.
There is a developer option in android-Destroy Activity as soon as user leave it.
So When i check that activity and run my launcher activity having single top flag.I have seen that both my onCreate and OnNewIntent was called when I launch app after pressing Home Button.
Can Anyone explain that why both the functions was been called and also It was called only for my launcher activity .Rest all activities were working fine.
i'm trying to show an add on my app .
i want to show the add every time that the user launches the app or if the app is in the background when the user relaunch it.
i added the function that shows the add in the onRestart() event and it seems to work fine.
my problem is that if the user goes back to a previous activity by pressing the android Back button it triggers the onRestart() event as well.
is it possible to determine if the activity was loaded by the back button?
Thanks alot
Avi
From what I understand, here is what you can do:
If you are starting an activity from another activity, instead of calling startActivity(), use startActivityForResult(). In the called Activity, override the onBackPressed(). Put some data in the returned intent and call super.onBackPressed(). In the caller activity you will then analyze this to know that back was pressed :)
Also, move the advertisement to the onResume() method which is called when the Avtivity becomes visible to the user.
I suggest you to put your add in the Onresume() method of your main activity;)
You're on an activity and you press the home button.
Then you long press home menu button, and select the activity you were on, from the 'recent activities' screen.
What method is called when the activity shows again? onResume, onRestart or any other?
I believe onResume will be called anyways even after pause or stopped.
onRestart may be called if activity has been stopped in the background
The recommendation is to save your data in onPause and rebuild it on onResume with some flags, so flags can tell you if onResume called after onPause/onStopped or Activity is freshly created.
Taken from the Android developer website
"... When the user leaves your activity, the system calls onStop() to stop
the activity (1). If the user returns while the activity is stopped,
the system calls onRestart() (2), quickly followed by onStart() (3)
and onResume() (4). Notice that no matter what scenario causes the
activity to stop, the system always calls onPause() before calling
onStop()..."
Here is the Activity
So no Matter what onResume() would get eventually called.
You can download the ActivityDemo which exhibits the Android lifecycle accurately. This should help you.
I need to simulate a part of the lifecycle, from onPause to onResume event.
I used the back button to pause the app, when I enter the app again, it always to the onCreate event to start a new lifecycle.
How to make the app run from onPause to onResume directly ?
Thanks.
well android use back button to remove activity from stack.
but if you don't want to remove activity from stack you can use home key for returning from app.
but if you still want back button to work like home button then you can refer here
try opening a dialog on press of a button. This will call the onPause. Close the dialog onResume will be called. Start a new activity onPause followed by onStop called. Press back onResume called