guillaume
guillaume

Reputation: 1678

Handle data between activity

Im working on a new project to learn android programmation. So I want to know how can I handle data between Activity.

I have a MainActivity which parse a remote XML file and put all parsed data in a List Moreover MainActivity displays a list of all MyData, and if I click on an item it's start my DetailActivity.

But now I use a putExtra with a Parcelable of the MyData item to display datas (only text). So I want to know if it's the right way ?

And I've another question. In MainActivity I handle one remote XML file, but if I have an activity SecondActivity which handle a second remote XML file. How can I do to download these two files only once, to avoid many download when I switch between MainActivity and SecondActivity.

Thanks

Upvotes: 0

Views: 217

Answers (2)

Nikola Davidovic
Nikola Davidovic

Reputation: 8666

In your place, I would make Singleton class that will hold the data that is parsed from the XML, that way both Activities can access data. Dealing with Parcelable is difficult and should be used only with simple data types. Also I have read that this approach is recommended. In your case, parse XML in a separate class. Since you are using a List than your data will be placed in a ArrayList or Array. The only thing you should pass between Activities is an index of data for which you would like to display details and retrieve it from the ArrayList that is in a Singleton class. I have used this kind of approach, moreover you can access data this way from any Activity in your Application.

Upvotes: 0

toadzky
toadzky

Reputation: 3846

Save the files to the phone to avoid repeat downloads. Just check for the existence of the file and if it's not there, download and save it. As for passing data between activities, the Intent's putExtra method is the correct method. I prefer serializable to parcelable, but that may be because parcelable is harder to implement.

For a simple example of the putExtra method, please look here: StackOverflow answer

Upvotes: 4

Related Questions