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.
Related
Currently I'am developing app which having a bad API. I have a situation where I have to use data received from network call in a activity, which is 2 steps away from the activity where I made the network call. In other word all the data necessary fill 3 activities comes on this one network call.
So I pass necessary data between activities using Intent Extra. It is not easy to maintain and It takes noticeable time to switch between activities.
I know one possible solution is to store data in database. and access those data from different activities. It feels like a bad practice because no use of those data after user pass 3rd screen.
Is there any better technique to handle this?
You can put all your network logic in the separate class, cache data in some variable and use it in your activities (you can use singleton class or injecting by dagger).
Also you can read about Clean Architecture and get some good practices from it.
If you don't want use anything from above, you can remove data from database after using and not store it forever. Or you can use SharedPreferences if your data is not complex.
if save button clicked the selected values should be saved in first api.then back to product add to cart page, before selected values available in that page.is it possible in e-Commerce android mobile apps.
Okay. I think you mean "how to pass data between two activities".
Activities are java classes, but you usually don't use them via constructor. Instead of that, we use what we call an intent: a generic builder which allows us to send zero or many arguments to the activity class, using what we call a Bundle, a datatype very similar to a Map. So, to communicate the first activity with the second, you can add the desired data to a Bundle, add that to the intent, and then, in the second activity, take the data.
Now, for this kind of fluxes, you perfectly can, and should, have a separate data storage layer which all activities can consult. And you can use compound views or fragments to avoid loading different activities for trivial tasks.
The data storage layer advice also works if you wish to combine several remote responses into one. Just make all the network communication in a separate layer, then store the results and provide the answers to the views (activities). Remember, apps are not webpages, and people expect them to work even if the internet connection fails.
Hi to anyone who can answer,
So I came across this question (it was an assignment, I already submitted it anyways). It was regarding shared preferences and explicit intents. I know both can pass data (through putString, putExtra, putInt etc and getExtra, getString, getInt). However which method is better to pass data and why? It can be in terms of functionality or just how much lesser the codes are when comparing each method to each other.
If you want to pass data when transitioning from one activity to another activity, then it's normally better to use intents to pass data.
However if you want the data you are passing to still be retrievable after the user exit your app and reopen it, then you should use SharedPreferences.
Intent is better to use when you are passing data when you are going from one activity to another. Otherwise, you should use SharedPreferences instead. And as mentioned above, if you want the data to be stored and retrievable even when the user reopens the app, then you should always go with a storage option like SharedPreferences.
those are two completely different functionalities that should not be mixed.
Intent extras are used to pass data from one activity to another. If the intention is having one activity put data and the next one receive you should use Intent extras
SharedPreferences is a very lightweight data storage. It's meant to store data on permanent memory and can be retrieved by any entity within your app. If you need data to be saved and access any moment in the future from anywhere in your app, you should use this.
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.
I need to share data between a local service and its hosting activity,
I am using sendBroadcast in order to send data to the hosting activity from the service, but what if I wanted to send data back to the service from the hosting activity?
So I though of creating a static method on the service, and call it from the activity, and through it send the parameter, but then I can't do operations like show a toast inside that static method (which is inside the service)...
This is because you can't use myclass.this inside a static method, I guess there are more limitations...
Maybe there is another solution? Maybe there is a proper way for this task?
Thanks,
moshik.
You could create a sharedpreferences file and store the data in there. That way both activity and service will be able to access and update the same data
I'm not too sure on your use cases, however another option (aside from using intents / shared preferences) is to extend the Application class and put your object onto that.
The benefit of this is that you don't need to worry about intents, but it may not be the best option for your scenarios.
I wrote a tutorial here, give that a try
*disclaimer : this is just one way to solve an issue, not THE way..