In my application I have a service in which rss feeds are read. After the service finishes the reading I need to update my application and populate a list view with the downloaded information. Using a broadcast receiver, and intents would not work, because the data is stored in a custom object. What is the best way to do that?
Using a broadcast receiver, and intents would not work, because the data is stored in a custom object.
Then don't store the data in "a custom object".
What is the best way to do that?
Step #1: Use a database.
Step #2: Alert the activity that there is new data in the database, via a broadcast Intent, or via a Messenger passed into the service, or via a PendingIntent created by createPendingResult() passed into the service, or via a ContentProvider facade and a content observer, etc.
Step #3: Have the activity load the data out of the database, perhaps simply by calling requery() on an outstanding Cursor.
1) The simplest way to get Custom class data in Intent is, Get the data to primitive types then put this data by putString or whatever data type, then add a constructor to corresponding sent data to activity from set in custom object in activity.
2) Other way of doing this is extend your custom class by parcelable and override corresponding methods then you will be able to send parcelable class object with intent.
go to this link How to send an object from one Android Activity to another using Intents? for details
Related
I'm trying to pass the data from one activity to another activity without opening the activity. I tried it by not writing startActivity(intent) part but it fails.
How do I pass the data from one activity(1) to another activity(2) without opening that activity(2)?
Activities are UI elements so your question makes no sense. If you want perform some action that does not have UI create a service to perform the work and start or otherwise call that service from your first activity.
If you want to make data available to activity 2 when it does start, pick a persistent storage mechanism and write the data there, then read the data when activity 2 opens.
https://developer.android.com/guide/topics/data/data-storage
Alternatively, you can create a custom Application and store the data there and share it between activities.
Just a word of advice. You should describe what you are trying to accomplish not how you want to accomplish it. Activities are not the right tool for this task, but we can't suggest the right tool because we don't know what you want to build.
You can use events for passing around data. Either use the android native LocalBroadcastsSytem and put data as parcelable in the intent, or you can use any event library like EventBus
Pass the data from one activity to another activity without opening the activity try to do with the broadcast receiver or SharePhreferance or make that data public static You can access your data in anywhere of the application.
I have the following situation: I need to save data from an Intent's putExtra call into another Activity. I am able to display the putExtra data, however I cannot save it. I am using startActivity to start the activity that I need to save the data in. I am attempting to store it by adding the data to an ArrayList, however whenever I change Activities the data is lost. Also it only stores the last value. How can I store the sent data in the second Activity?
You can store it in the Application Context as described here:
Static way to get 'Context' on Android?
Hello fellow developers,
I am trying to achieve something in Android and I would like some advise on the best practice.
I have created an Activity which can start and stop a Service which collects data.
Instead of simply starting and stopping the Service, the Activity should also display the data collected by the Service.
Here in lies the problem. The data could be quite large so I would like to avoid Serializing and sending it via an Intent. Is it possible to simply get a reference to the data stored in the Service from the Activity?
Simple Example
1) Activity starts
2) Activity starts Service to collect data
3) Activity exists
4) 24 hours pass
5) Activity starts
6) Activity wants to display data collected by Service, but data is quite large.
My question again is simply this. Can the Activity get a reference to the data stored in Service or does the data have to be Serialized and sent from the Service to the Activity using an Intent?
Kind regards,
Cathal
Research android.app.Application class. Essentially you can use this for global state.
http://developer.android.com/reference/android/app/Application.html
Also, any long term data should be stored to a local database. I would try to limit the SELECT to certain number of records to reduce memory footprint.
I've heard that the Application class should not be used in this way necessarily (via Google Team). Instead you could make a separate Singleton class (For example call it "MyData") that holds a public static reference to a let's say ArrayList (For example called "list_data").
So when he activity starts it will call
MyData.list_data = new ArrayList<MyDataObj>();
Then in your Service you can add all of your data objects to this static arraylist like so:
MyData.list_data.add(new MyDataObj());
This allows you to work off of the same ArrayList without passing it around using Intents and etc. Although I would suggest doing some null checking whenevr
I'm not very experienced in android specific components, so I just can't get where I need to look.
I have three activities:
1st gets info about some groups of objects, user selects some of this groups and activity needs to star next activity
2nd activity shows all objects from groups, that user selected at previous activity, here user can deselect some selected objects and then activity starts 3d activity
3d activity allows user to do something with selected objects
My solution is make some singleton model, that let activities get and save information about objects,
but i suppose, that android has some special components to provide data between activities
Help me to find this components^ please
It depends on the type of data that you want to share:
Primitive Data Types
To share primitive data between Activities/Services in an application, use Intent.putExtras(). For passing primitive data that needs to persist use the Preferences storage mechanism.
Non-Persistent Objects
For sharing complex non-persistent user-defined objects for short duration, the following approaches are recommended:
The android.app.Application class
The android.app.Application is a base class for those who need to maintain global application state. It can be accessed via getApplication() from any Activity or Service. It has a couple of life-cycle methods and will be instantiated by Android automatically if your register it in AndroidManifest.xml.
A public static field/method
An alternate way to make data accessible across Activities/Services is to use public static fields and/or methods. You can access these static fields from any other class in your application. To share an object, the activity which creates your object sets a static field to point to this object and any other activity that wants to use this object just accesses this static field.
A HashMap of WeakReferences to Objects
You can also use a HashMap of WeakReferences to Objects with Long keys. When an activity wants to pass an object to another activity, it simply puts the object in the map and sends the key (which is a unique Long based on a counter or time stamp) to the recipient activity via intent extras. The recipient activity retrieves the object using this key.
A Singleton class
There are advantages to using a static Singleton, such as you can refer to them without casting getApplication() to an application-specific class, or going to the trouble of hanging an interface on all your Application subclasses so that your various modules can refer to that interface instead.
But, the life cycle of a static is not well under your control; so to abide by the life-cycle model, the application class should initiate and tear down these static objects in the onCreate() and onTerminate() methods of the Application Class
Persistent Objects
Even while an application appears to continue running, the system may choose to kill its process and restart it later. If you have data that you need to persist from one activity invocation to the next, you need to represent that data as state that gets saved by an activity when it is informed that it might go away.
For sharing complex persistent user-defined objects, the following approaches are recommended:
Application Preferences
Files
contentProviders
SQLite DB
If the shared data needs to be retained across points where the application process can be killed, then place that data in persistent storage like Application Preferences, SQLite DB, Files or ContentProviders.
yes, you can send data between activities using Intents. Using the putExtra() function for that. If you want to pass your own objects, you need to implement Parcalable class.
There is no problem with using a singleton to share information between your Activities, especially if you need this data to be consistent throughout your whole app.
Alternatively you could use an Intent to pass data between Activities - putExtra().
Yes it does:
http://www.remwebdevelopment.com/dev/a33/Passing-Bundles-Around-Activities.html
Basically you can either send a Bundle or just use putExtra function for that.
try this way please
Intent i = new Intent(this, YourTragetedActivity.class);
i.putExtra("value1", "test1");
i.putExtra("value2", "test2");
startActivity(i);
//// On Your TragetedActivity
getIntent().getStringExtra("value1")
getIntent().getStringExtra("value2")
I have an application that uses a service to create an ArrayList of custom objects (MyObject) every x seconds. I then want my Activity to obtain this ArrayList.
I'm currently planning on having the Service send a message to the Activity's handler every time it finishes the query for the data. I want the message to the Handler to contain the ArrayList of MyObjects.
When building the method in the Activity to get this ArrayList out of the message, I noticed that I couldn't.
If I tried
msg.getData().getParcelableArrayList("myObjects")
Then the method I was passing it to that expected an ArrayList wouldn't accept it. If I tried casting the results:
(ArrayList<MyObject>)msg.getData().getParcelableArrayList("myObjects")
I received the error: Cannot cast from ArrayList<Parcelable> to ArrayList<MyObject>
MyObject implements Parcelable and I have successfully sent an ArrayList from my service to my activity by having my activity call a method on the service to retrieve it. I'm trying to go away from having my activity poll my service for this data, though.
1) How can I send an ArrayList inside the bundle in a message to handler?
2) Is there a different model I should be using to have my service update the data in my Activity that may or may not be visible? I always want the data in my activity to be the latest from the Service.
I had the exact same question and while still hassling with the Parcelable, I found out that the static variables are not such a bad idea for the task.
You can simply create a static field
public static ArrayList<MyObject> myObjects = ..
and use it from elsewhere via MyRefActivity.myObjects
I was not sure about what public static variables imply in the context of an application with activities. If you also have doubts about either this or on performance aspects of this approach, refer to:
What's the best way to share data between activities?
Using static variables in Android
Cheers.
There is another model that should be used. Another question I asked provided the answer:
Suppress notifications from a service if activity is running
As for #1, you could get around it by just removing the generics from the ArrayList declarations and casting as appropriate where needed. I know this works because that's what I did before refactoring based on the other question asked.
If the cast is the problem, just leave it be, dont cast it, the error will go away.