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.
Related
My app get minimized when I press the back button on the home fragment, which is fine, but when I open it back up it shows me a blank activity. I guess it's not loading the fragment that it's supposed to load.
I added a text view with some text on the activity but it's not showing me that so I'm seeing something else.
How should I manage this issue?
Your activity has a lifecycle. If a lifecycle state is changing, then a method will be called. If you open your app, after you minimize your application, then the method "onResume()" will be called. Try to use this method in your activity to check, whether your activity is the right activity.
UPDATE(Thanks to Santiago Gil):
It's necessary to call finish() on the onboarding activity.
I used toasts and found that the app was returning to the launcher activity, but it was not going through the onCreate so it appeared blank, I now call finish() on the onboarding activity and so far it's working fine. Thanks
I wrote an Android app with several Activities and a Main Activity. When I go from the Main Activity to lets say Activity B, I want to "pause" the Main Activity, when the back button is pressed it should go back and reactivate the Main Activity instead of calling onCreate().
This shall work with all Activities, so if I click on a button to start Activity B, it shall also reactivate the old Activity B instead of creating a new state with onCreate().
How can I realize this?
PS: I already tried it with parcelable, but this do only work, if I close the application or something unexpected happens.
It's always possible that the first activity may be destroyed when you start another activity. It will be recreated when you go back to it. Every app should be written with this possibility in mind. To make sure your activities can handle being destroyed and recreated, turn on the "don't keep activities" developer option.
It is by default in Android. If you Start Activity B from Activity A then activity A goes in stopped state. Below methods will be called of Activity A
onPause()
onStop()
When you tap on Back key on Activity B. Below methods of Activity A will be called.
onRestart()
onStart()
onResume()
For reactivating Activity B, you can set Activity B launch mode as singleInstance. This will make sure that only single instance of Activity B will be created. onCreate will be not be called again. onNewIntent will be called when that activity is reactivated.
Refer: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Activities live in stack order. Each activity has its life cycle. When you close an activity (Activity B in your example) it eventually reaches onDestroy() method and removed from the stack order. It is by default in Android and there's nothing you can do about it.
What you can do is rewrite the onStop() method in Activity B and save some activity data (like text in EditText, for example) in SharedPreferences - read here. Then in your onCreate() method you can call for SharedPreferences, check if it's not empty and restore the text in the EditText by pulling appropriate value by key.
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;)
I am making a activity using Tab-Host.
I have two tabs. When I start the tab-Host activity, the tab-Host opens the activity and the life-cycle of the activity is calling but when I changed the tab and again open that previous tab the activity is not getting its callback methods like resume.
I don't think there is any specific reason it should restart. For changing configuration (like rotating the device or sliding out a keyboard) there is a specific trigger because the app needs to deal with the change. But any other process should go according to the Activitvy lifeCycle
When your app goes to the background (looses focus) for any reason you get onPause() called, and when it goes back, your onResume() will be called. This is the same for when you go home and then back to your app, or when you switch activities like that. No new intent or something like that, just going back to the activity.
You should put your code that needs to run in the onResume().
Do what you need to do in the activity in onResume() instead. That will get called everytime, not just the first time it is created.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
When you switch from one tab to the other and back, the first tab only gets its onResume method called since it has already had its onCreate called the first time.
You can run the code you like in your onResume method if you want anything specific to happen when it gets focus again.
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