0

Sorry for my amatuer question, but I'm really new to Android, and even Java.

I have a listview layout in my appwidget. The listview items are handled in widgetservice.java, where i declared an array to store the listview items in.

There is a configuration button for every listview item. When you click on them a new activity starts, and i really need to acces the listview items in this activities (so actually an element of the before mentioned array).

I did some research how to do that, and I came accross with the Parcelalbe class, so that i can attach a Parcel to my Intents. That is great, however the parcels seem to be just mere copies, and i need my original array elements (so i can change them in the activities, etc).

I hope you could understand my problem, and would able to help me. Cheers.

Bertie92
  • 697
  • 1
  • 9
  • 17
  • If the data only exists in the Activity with the list, then you could call the 2nd Activity and ask for a result (`startActivityForResult` http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int) ), and use that result to modify the data in your 1st Activity. If the data is stored, you could pass an identifier to the data that needs to be configured, update it, and when returning to the 1st Activity refresh the info to reflect the new stuff. – frozenkoi Jun 22 '14 at 00:51
  • This was helpful, but didn't help me. I have a widget (and not an activity) which starts a new activity on button clicked :( – Bertie92 Jun 22 '14 at 10:40

1 Answers1

0

Parcelable is one way to go but unless you are just dealing with basic, single dimensional, String only Lists/Arrays than I wouldn't go down that route as it can get complicated quite quickly and is not hugely flexible.

The preferred way to go about this is using Fragments. Such as the ListFragment in this case.

Your ListFragment contains all the bits and pieces it needs to display a ListView. It should generate (or be able to access) it's own collection of items and know how to display and control them. The benefit of putting it all into a Fragment is that you can then easily reuse it in any Activity or layout.xml you like and make use of the same functionality again without having to duplicate all that code.

If you need your ListFragment to be dynamic and not just display the same list all the time you can pass it some parameters through a newinstance(...) method. That method will itself wrap all the info and arguments you want into a Bundle object that you will use to configure or populate your Fragment. Here's how to go about that.

What that ensures is that if your Fragment needs to be recreated (screen rotate, Activity change, Application focus etc etc) it will be able to recreate itself properly using the same parameters again without you having to intercede.

It may seem like more work now to try to get your head around the concept and use of Fragments but in the long run it will save you time, spare you headaches and let you write more maintainable and appealing code.

Community
  • 1
  • 1
indivisible
  • 4,892
  • 4
  • 31
  • 50
  • As I can understand from the Android guide this is very helpful, but i basically have a widget that starts a new activity with the configuration button and not an activity that starts another activity. – Bertie92 Jun 22 '14 at 10:42