0

I have an activity which includes a declaration of

public String progressArray[][] = new String[3][3]

In another file (a service) I want to access the values of this array. I code:

ListViewLoader lvl = new ListViewLoader

Now I find that lvl.progressArray[0][0] returns null although I know that data has been put in it in its own class. Is this because a new instance doesnt contain the values of the original, if so how can I access the values in the original?

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
ron
  • 5,219
  • 8
  • 39
  • 44

3 Answers3

0

If you are expecting the Service to always be started by that Activity, you can send the String data to the Service using the intent which starts the Service. You achieve this using the putExtra and getExtra methods:

Intent serviceIntent = new Intent( this, myService.class );
serviceIntent.putExtra( "pa_0_0", progressArray[0][0] );
serviceIntent.putExtra( "pa_0_1", progressArray[0][1] );
//...
serviceIntent.putExtra( "pa_2_2", progressArray[2][2] );
startService( serviceIntent );

You can get the Strings back out again in your service by overriding its onStartCommand method, which includes the intent that started the service as a parameter.

   progressArrayCopy[0][0] =  mIntent.getStringExtra( "pa_0_0" );
Jems
  • 11,560
  • 1
  • 29
  • 35
  • Thanks - I should have explained that I prefer to be able to directly access the original instance created by onCreate in the class. I have a sort of hazy idea that maybe context is the way but if so I cant see how to make it work for me. – ron Feb 05 '11 at 15:41
0

Hmm...sounds more like a programming problem rather than Android related. If you have a class and you have an array declaration as in your case above, any new instance of that class won't contain a new instance of that array as well. Say you have a class that is used by some other class and you set its value as class.progressArray[0][0] = ... and then in some other class you want to access those stored values, you have to have the same instance of that class in order to get the stored values back.

Normally in programming you have different possibilities:

  • you pass that created instance containing the data around, i.e. through the constructor or some public setter method or
  • you create a static class/static property on that class which will be the same on all instances (caution with that)
  • you create a singleton, meaning there will be only one instance throughout the whole life cycle of the application.
Juri
  • 32,424
  • 20
  • 102
  • 136
0

Your solution is not working, because progressArray is an instance variable and while calling

ListViewLoader lvl = new ListViewLoader

which creates new instance with progressArray set to default value (null in that case). Making progressArray static can be a fast solution, but be aware you will highly depend on order in which activity and service will access that array and it may be reset when your activity is destroyed by system. I'm strongly discouraging you from doing that. Instead, please consider extending Application class and implement list initialisation / access there, like:

public class MyApplication extends Application {
    public String progressArray[][] = new String[3][3];
}

Then declare it in AndroidManifest like:

<application
            android:name=".MyApplication"
            android:label="@string/app_name"
            android:icon="@drawable/icon">

Then both, from your activities and services you can get that by calling

((MyApplication) getApplication()).progressArray

This is the only way to share state between activities and services across one application I'm aware of, neither involving sending messages back and forth to keep state consistent (which can be nontrivial task) nor using singletons (which is anti-pattern as you probably know).

But please keep in mind, that need of sharing mutable data in that manner may be a hint, that your design needs an improvement.

mcveat
  • 1,416
  • 15
  • 34
  • Thanks that has cleared up my confusion re access. I have looked again at the design. Part of my problem was that I had await period whcih – ron Feb 06 '11 at 11:35
  • Ooops continued a sleep period whcih was proving to be unreliable and so I have been trying all sorts of alternatives. Finally I settled on a repeating alarm which seems reliable and a listener which is in the same class as the array so that I now have access to the original instance. Thanks again for the explanation re access , very useful – ron Feb 06 '11 at 11:38