Android: What happens when variables passed using intent - android

I want know i pass a variable via Intent to another activity and that activity changes that variable, will it reflect in original activity without passing back the intent.
If answer is no then is it better to use global variable using application then passing intent and getting back data. in my program, i am having round 5+ activities and all of them need to access a list of class objects.
any recommendations apart from above

Create your own extension of Application to store the state of your app and share data between the different activities that make up your app. The Application acts as the context for your whole app and Android guarantees there will always only be one instance across your app. Hence it works similar to defining your own Singleton, but using Application will allow Android to take control of the life cycle of your shared data and basically do the memory management for you.
To summarize:
Create your own subclass of Application.
Specify that class in the application tag in your manifest.
After this you will be able to safely cast the result of all call to getApplication() (from an Activity instance) and getApplicationContext() (from any Context instance) to the subclass you defined in step #1. This means you can use any getter/setter method defined in your application extension to store/retrieve data.
Have a read here for more details.

Related

Android - how to pass data to application startup code (i.e. onCreate)

I have an interesting use case where I need to pass certain data to my application so that it can be used in its "attachBaseContext" method.
I know that typically, the proper way to pass data to an application is to use an intent and pass the data through to an "activity" but this is too late in my application's life cycle. I need the data in my application so that I can use it to setup the environment prior to any android components running (e.g. Activities, Services, ContentProviders, etc.).
At the moment, I use a very hack-ish approach where I write the data to my application's internal storage and then read it in my "attachBaseContext". I am hoping there is a better way though to do this as this doesn't seem very safe/clean.
Any ideas?

Android: Why Parcel?

I'm a C/C++ guy, and I'm quite new to Java and Android.
So I'm creating a class instance in Activity "A" and I'm trying to use this instance to Activity "B". In C/C++ you would simply pass the address (pointer) of the instance.
Now I learned that with Android I should use a Parcel, but I don't get the idea: why would I want do all that parceling/deparceling procedure when all I want to do is passing a pointer?
Please enlighten me!
This is related to the Android activity model.
Since your app may be killed by the system to recover RAM (this should only happen when it's in the background, unless the system is in a really bad situation), an object reference just won't do. It'd be completely worthless once your process dies.
The intent you use to start activities is saved by the Activity Manager, so that it can be used to recreate your activity when the user navigates back to it. That's why all the data in your intent has to be parceled.
Keep in mind that this means that your two activities will have two similar instances -- but naturally, they will not be the same object, so changes to one won't be reflected in the other.
Parcel or parcalable is an efficient and alternative way to serialize objects in Android. Its an alternative to Serialization in core Java.
Java Serialization API allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage.
Its a way to exchange objects between sender and receiver(Class). Receiver than deserializes this object.
In Android parcel is used to exchange objects between different activities by using Intents.
java doesn't work like c/c++, you don't work directly with the memory. If you want to pass an object from A to B, you have to set it in a Bundle, and add that bundle to the intent. if it's a custom class, let's call it C, C should implement parcellable. Java works by value, not by reference.
custom parcelable objects
how do i pass data between activities?

Where to store data used in different Android activities?

In my application, I have one activity, which connects to the server and gets certain user information from it. Then, it launches another activity where that information should be processed.
What is the correct way to pass data between activities in Android?
If that data is only needed once, you should use an Intent. Here is a tutorial on it.
If it needs to be accessed all the time, you can put it in an Application subclass or some other singleton.
In general you can use methods like putExtra, to attach data to an Intent, and then use
for example getIntent().getStringExtra(name) to retrieve it in the other activity.

Android pass persistent information in bundles or use singleton pattern?

Just wondering what is a better practice to pass information between activites, adding it to a bundle or using a singleton class to store and access this data. I have used both in the past for various android side projects, but I am now working on an android project that is of much larger scale, so would prefer to do things right towards the beginning.
My application authenticates users and then will have to do various queries based on it's id. To minimize coupling between activities, I would think just adding the id to the bundle, and then letting each activity query for the information that it needs, would be the best bet; however to increase responsiveness, I was leaning towards using a singleton class to store persistent information, preventing more queries than need be.
Personally, I would create an extension of Application to store the state of your app and share data between the different activities. The Application acts as the context for your whole app and Android guarantees there will always only be one instance across your app. Hence it works similar to defining your own Singleton, but using Application will allow Android to take control of the life cycle of your shared data and basically do the memory management for you.
Here are some more details. If you go down this path, you can simply add any getter/setter (or other) method to your application extension to store/retrieve data and do operations on it. Especially the latter can become quite a pain to manage (and keep consistent) when using Bundles passed back and forth between activities. If would only use a Bundle if the data is needed in just one or two places that are neighbours in the activity flow and does not need any (complex) operations to be run on it.
The better way to go for you is to use SharedPreferences to keep userId you need to keep and reuse. Of course you can use singleton approach or even Application class, but the data will be lost after application is killed.
The only time I pass data between Activities via bunlde is if it's something that I won't need to access for a while(i.e the the resID of a resource I want to use only once in the calling activity, etc). I would also think the difference in responsiveness would be very minimal, so that shouldn't be of concern. I suggest the singleton approach
Passing bundles is a tedious job. You'll have to pass a bundle for every change in activity to make sure that the value is not lost, even if you're not using the value in the called activity.
Singleton pattern have some bad results. For example:From Main activity you call secondary activity. Phone call interrupted your work.After ending phone call Android is trying to bring secondary activity to screen. Here is my nightmare - many users complaint about exceptions - Google reported to me NULL pointers in my singleton. So you have to provide not only singleton, but all data inside singleton have to be as singleton too. This maked come very complicated :(

Android Application Initialization

I have an application that is driven by a configuration XML: various
app properties are loaded at the app start-time by parsing the XML and
initializing static variables of some class. The data read from this
XML drives different Activities of the application. Presently, I have
called the "parsing and the properties-initialization" from the
onCreate() of my Main Activity.
I have a few questions as regards this case/approach:
Should I invoke the app initialization method from the Application
Object or is the current approach correct? What advantages/
disadvantages do/would we get/have if I choose to invoke it from the
Application object?
Do we really need a static class to store app properties? Or can we have all the properties as a static Collection variable in the application object?
Parsing a XML(~200 nodes) at app load time might take some time(not
sure how long tho); How can I avoid the dreaded ANRs? I am using a
Pull Parser.
Please help me find answers to these questions.
Thank you.
It depends on what you're initializing. Application's onCreate() should be used when you're doing things that need to be done before any part of your app works correctly and only needs to be done once, whereas Activity/Service/etc's onCreate() should be used for things that are needed for that component alone and needs to be done multiple times.
The main concern I have for putting all your initialization into a component is that it will make extending your application more difficult later on. Suppose you want to make some Activity in your application accessible by outside intents - now you've got to either move the initialization code to Application or you have to duplicate initialization code in the non-launcher Activity.
It sounds like you should check out SharedPreferences, especially PreferenceManager.getDefaultSharedPreferences(). The preferences will be stored between sessions and it gives you easy access to simple properties from any Context.
Threading. I find AsyncTask to be the easiest way to accomplish this task; there's a good write-up on it at Google. Alternatively, you could fire up a Service to do this in the background while having a foreground Activity inform the user that you're booting up the app.
The Application object is used for sharing non-persistent state across the application. I don't think you'll need to use an Application class at all. You can do your initialisation in the onCreate() method of the Activity that is called first. To quote the documentation:
The subclass is optional; most applications won't need one. In the absence of a subclass, Android uses an instance of the base Application class.
You don't need to create your own class to store application properties. This is done for you by SharedPreferences.
You should also have a look at the setDefaultValues() method in the PreferenceManager
class as this will set preferences from the data in an XML file. What's nice about this method is that use the readAgain parameter so that the XML is only parsed once - the first time you start up your application - rather than every time.

Categories

Resources