0

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

guillaume
  • 1,638
  • 5
  • 24
  • 43
  • if `MyData` only uses simple data types let it implement Serializable and send it between your Activities that way (using `putExtra`). – Blundell Oct 16 '12 at 21:38

2 Answers2

4

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

Community
  • 1
  • 1
toadzky
  • 3,806
  • 1
  • 15
  • 26
  • Right. You can also cache the [ETag](http://en.wikipedia.org/wiki/HTTP_ETag) if your server supports it, and then send an If-None-Match to avoid repeat downloads. – dokkaebi Oct 16 '12 at 21:41
  • I think i'll retrieve xml file on the phone. But if I do it file will be only accesible by my application ? Now, I'm using parcelable and I think it's not really harder to implement, at least in my case it's work well for what i'm doing. – guillaume Oct 16 '12 at 21:50
  • well, to implement serializable, you add `implements Serializable` to the class definition. that's it. as for the files, if you create them using the `openFileOutput` method from an activity, you can specify the mode. use MODE_PRIVATE to keep it local only – toadzky Oct 16 '12 at 21:53
  • I read somewhere parcelable is better than serializable so that's why I implement it :) But the difference is huge two words only for serialize against many method for parcelable. I think i'll try to download my xml file in private and remove them every days because I'll need to download remote file every days because they will change at midnight. Now i'll think about a way to handle my data. I think a class which handle all my data at least an abstract class – guillaume Oct 16 '12 at 22:08
0

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.

Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33