I am calling a method that populates a spinner from files in onCreate Method? As per the life cycle of andriod goes after it stops or if i use another apps it shouldnt populate spinner. however it is populating. Can you tell me why?
Once it is populated in onCreate(), it will stay populated unless you quit the activity. If you use some other app or just press the home button the activity may not be destroyed. It might simply be in the paused state. Once you come back to your activity it will simply resume itself to the last state it was in i.e. the spinner will remain populated. Android might decide to kill activities in the paused state as well if memory is low.
If you want to quit the app, Press the back button while you are in the activity. Then the activity will probably be destroyed and when you open it again, onCreate() will be called and the spinner will be repopulated.
Related
I'm having a hard time managing a back button in my app. If I'm not mistaken, the back button default function should return the user to their previous activity. But when I'm changing from one activity to another I call finish() in the listener. Because of the killed previous activity, pressing back button causes the app to exit.
Is there any way to preserve the previous activity and kill it only after the current activity has changed?
BTW, I know how to override the back button. But if I have a lot of activities, is it efficient to write an override for every activity that doesn't have previous acvitity already killed? I'm developing an RPG and I'm pretty sure there will be a lot of activities.
You're setting yourself up for a really bad time. You want to use as few activities as possible. In a game you really don't want to manage all of that data going back and forward between * amount of activities.
You need models to manage the game data, and your view activity can receive this data from a view controller. You don't want to kill you main view activity and you don't want to keep a bunch of activities in memory. Read a few chapters from this book. Good luck!
http://danielrparente.files.wordpress.com/2013/01/rpg_design_patterns_9_26_05.pdf
I have a problem with Activity life cycle. In my server communication Activity I am downloading the list of items from the server and then setting up the Adapter for the ListView.
Everything is fine but if I press Home button on this screen and after some while (e.g. 3 hours or more) return back to the screen via application manager, application crashes. The problem is in the onTextChanged() method (which is usefull for searching via EditText) where I am calling the setAdapter() method again. There is nullPointerException because my array was somehow erased.
Why is the onTextChanged() method called again during restoring? And why was the array erased?
Thank you for your help.
Please check the activity lifecycle diagram on:
https://developer.android.com/reference/android/app/Activity.html
Your Activity goes to Pause state after you press Home.
And after 3hrs or even a shorter time,it may be killed by system for resource management.
So it need to be created again on next launch.
I think you should add code to handle onDestroy() and onStop().
Retrieve the data the same way you did originally, by overriding the
onResume()
method, checking if the data is existent or not beforehand.
I have an application that navigates to the same activity but each time the activity loads with different parameters. In my application it's a parsed data content retrieved from url. First thing I want to ask: When I push the backbutton of my device I get my earlier activity without being recreated. Is the objects in this activities alive and can I reference them?
Second question is if my first question doesn't make sense, what do you advice me to do?
If you look at the Activity life cycle, you'll notice that as long as your phone has enough memory, your first activity is kept in memory, and with it any member with the data it contains.
But if your phone needs to have some memory, it can kill any activity kept in background (any activity but the one being shown to the user), which means that you'll loose any data that was in your first activity.
To know which happened, keep in mind that the onResume() method will always be called when your activity is brought to foreground (either on creation, or when you navigate back to it), but onCreate() will be called only when your application is created from scratch (meaning you don't have any saved data).
You should use the bundle mechanism to save data when your activity is paused, and load it when you come back to it. Read the paragraph about Saving Activity state in Android doc to see how to use this.
You are not guaranteed that in memory data will be around once you leave an Activity. Read through this part of the dev guide thoroughly to understand the lifecycle of an Activity: http://developer.android.com/guide/topics/fundamentals/activities.html
If you need to persist information, you should override the onPause, onStop, and/or onDestroy methods of your Activity. You can then save your state using SharedPreferences, the SQLite database, or even a flat file.
In the manifest file add the following to the activity:
android:launchMode="singleTop"
In your example what is happening is when you navigate back to the activity using the back button you are bringing up the activity from the stack. When you navigate to the activity inside of the app what is happening is a NEW activity is being created, while the original is still on the stack. The singleTop launch mode will pop the activity out of the stack if it is there when you navigate to it in the app, and only create a new activity if it is not on the stack.
Without singleTop each time you launch the activity in the app it will create a new instance and you will find there are times you have to hit the back button on the same activity more than once due to multiple instances.
I've been trying to figure out how to stop all threads when my application is paused (when the 'Home' button is pressed) and I just can't figure it out.
Here's what I have: one main activity containing a tab host, every tab host has a list, when one item in the lists is clicked a new activity with details is started. Each list is within its own activity which starts an updater thread that makes sure the list content is always up to date.
Here's what I've tried: I've tried using the onPause(), onResume(), onStop() and onDestroy() events. The problem is that if I use either the main activity of the activity that holds a list to monitor for those events, they get called every single time an item from the list is clicked, as well as when the 'Home' button is actually pressed, so there's no way to distinguish between the situations.
Am I missing something? How is this usually done?
This is a pretty good indication that your design is flawed. You shouldn't ever have threads laying around that you can't account for or that don't finish themselves.
Each list is within its own activity
which starts an updater thread that
makes sure the list content is always
up to date
This kind of thing should be done in a service.
they get called every single time an
item from the list is clicked
This is how those events work. As soon as your activity is no longer visible (ie your new activity comes in front), it goes to onPause() and possibly onStop(). You have to account for this. You can't fight it, or work around it.
I have two activities. One loads all rows from a database, and the other saves to the database. When I have the second save in onStop and the first repull the data in onResume, they do it out of order (the first resumes and then the second saves). I managed to fix this by putting the saving data in onPause, but why was this happening? Was this the cleanest way to do it?
Doing the save in the first actvity's onPause should be fine.
You've discovered that the foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time, the activity is in front of all other activities on screen and is interacting with the user.
When you start the second activity, onPause is called on the first and then interactive control switches to the second, with onStop on the first to be called somewhat in background.
This improves responsiveness and gets the new activity in front of the user ASAP. Consequently, you should try to keep your onPause implementation as fast and efficient as possible.
See the following Android docs for more details on the lifecycle http://developer.android.com/guide/topics/fundamentals.html, but what you have found should work fine for you.
Some official quote as an add-on:
The Activity documentation states that
onPause() is where you deal with the user leaving your activity.
Most importantly, any changes made by the user should at this point be
committed