Life of elements in an array of android project - android

I have arrays which I populate in one activity of the android project. Then I quit that activity to go to another activity. If I come back to activity 1, then will the array still hold values in them or would the values have been gone?

depends on if the system needed those resources to do something else. If it did then no your array will be gone. If it didn't then yes your array will still hold all of the same values.
See: Lifecycle of an Activity

Related

Different objects

In an app I'm making I have an Object to a class called Preferences in my MainMenu activity. I want to use this object in several pages. In the object is a class which contains an array with numbers. When I use it in another activity (Film) it changes one of the numbers in the object. Then I go back to the main menu and into a new activity (CurrentPrefs) in which I display the array of numbers on the screen. When I displayed the numbers they didn't contain the changes made in the Film file. So I started testing around with the variables a bit.
When I start the MainMenu, which is the start activity, it sets the object. Then when I enter film and check the object it still has the same name (object.toString()). I also checked to make sure that the values where changed correctly which they were.
Next I went back to MainMenu and checked again. The object still had the same name and the value were also like I changed them in Film.
Finally I entered the CurrentPrefs activity and there it goes wrong. The object suddenly has a different name and it also no longer contains the changes I made to it.
Why has the object changed and, more importantly, how can I make sure it doesn't change and stays the same everywhere?
There are any number of reasons why your object's values changed. Perhaps the previous Activity got destroyed and recreated without you realizing it.
You should check out Android's SharedPreference class. It is a way to store, edit, and retrieve preferences and have access to them in any Activity:
http://developer.android.com/reference/android/content/SharedPreferences.html
If you well know activity's lifecycle you can also understand why it happens. Probably some of your operation are delegated to onResume() method which is called whenever activity comes back from onPause() or immediately after onCreate(), while other operations reside in onCreate() called only when activity starts or after onDestroy(). I advise you to control this and trying to move all setText() in onResume() so that every updated object's variable will be read again.

How to pass list from one activity to other activity and all the time mantain same instance?

I don't know how to solve my problems. It goes like this:
In main activtiy I make my own objects and added them to list(arrayList) and after that when I go to another activity I would like to:
to send list of list og my own ojbects, I know you make it with: intent.putExtra but you don't have type list myObject
when I pass the list of lists of objects I think it makes new istance of these datas, but I would like to have one istance all the tame and I would like them to read and manipulate on first instance.
More explanation in my main activity I make objects and they are ready for all other activityes and all other activitys can read and write my list od lists. Only one activity is active in a time.
And I am also intetrested in when I manupilated the data in some activity and I would like to go to main menu and pick other activity how to send from that first activity to menu activity and pass them to next activity to process.
Would you help me, please. Best regards Robert
I suggest you implement an application object, and have the objects that you're referring to live in the application object rather than an activity object. There can be only application object associated with any particular app, and it gets created before any Activity objects, and is independent of whatever activity objects are created/destroyed during the lifetime of your app. Hence you don't need to worry about sharing your objects between activities, because they're all available globally in the application object.

Android/SQLite - "Managing Database in one place statically" vs "serialisation and intent method"

I have one general question:
what do you think about following two alternatives considering style and speed
having a cached list in the main application and always using this list and load data on demand (and remove it on demand, for example if the cache get's to big)
always serialise the data, adding it to the intent, give it to the sub activity, working on the data, serialise it again and give it back to the parent activity and replace the original data in the list with the one gotten back from the sub activity
in my case, i've probably always a list with a thousand entries... and about 5 levels into the deep... always serialising costs time... and i've to handle the changes, because the parent activity newer knows, if the data was changed in the sub activity...
the global data cache has the advantage, that I always and everywhere interact with the same objects and never have to take care, that if data is changed, that I update this data somewhere else...
Is that a bad idea? if so, why? I want to speed up my application and am thinking about changing it to this model...

retaining arrays

I'm creating quiz app of sorts for Android. The questions shall be posed in a random order, but every question should only appear once per run. For randomization, I'm creating a randomized array, with the index of each question in a separate class. This array is being passed on through an intent to the main class which is displaying the question.
Once the user has answered a question, I want to give him feedback. I do this by launching an activity telling him if he was right or wrong. After that, the question-activity is being restarted. The only problem now is that the randomized array is now gone.
How do I retain the array for later use? I really could use some help here :)
So you have two Activity classes: question-asking (A), and right/wrong (B).
If you launch startActivity from "A" to get to "B", then don't restart the "A" once you're done with "B". Simply call finish() on "B".
This will ensure that you do not lose your data from the question Activity, and additionally will not start hundreds of Activity objects.

includeed layouts - update in multiple activities

I have an app with multiple activities and multiple layouts. However, one piece of layout is included on several activities. I also have a thread which updates this layout. However, when i switch activity it doesn't work. Since the layout is included the elements have the same ID's, shouldn't it just work? Or do I really need to fetch an object for each element in the layout and feed it into my thread in order to make it update the elements in a new activity?
You should run the update code for each Activity/View, although the XML included is the same, each is a different instance.
My suggestion is on Restart verify is there is any modification to do in each activity, a simple way is to each Activity extend a BaseActivity that has this code.
I include a layout for adverts in my app, but on each activity that uses it, the adverts need to be reloaded.
If I call an activity from one that is using the same included layout when I go back to the previous activity it's still there.
I guess this is what you are seeing....
So you can also save that data inside sharedPreferences (if it is little data and primitive objets or parceable objects).
Also you can extend the Application class and store the data there and update every activity inside the onResume() method. that i believe is the best way to handle this. and this is quite simple to do.
Ask google about extending the application class and he will provide tons of results on how to do it. its an easy way to pass data between activities and/or keep a reference to a single object which you will use throughout the app. Just be carefull to clear it when you wont need it anymore because it will stay in existance untill the application is finished() (which comes with the application extension living thru the whole application lifetime).

Categories

Resources