开发者

Static objects in activity not accessable from service :(

I have a problem: I want to use static objects in several activites and a service (in an extra process). Unfortunately the service does not get the objects from the activity and v开发者_开发问答ice versa. I try to simplify where I think is my problem in code samples:

Activity "main":

public static myObject o1;
public static myObject o2;

public void onCreate(Bundle savedInstanceState) {
     o1 = new myObject("someParameter");
}

Service "myService":

private myObject o1;
private myObject o2;

public void onCreate() {
   this.o1 = main.o1;
   this.o2 = main.o2;

   this.o2 = new myObject("someOtherParameter");
}

Now the problem: for the main-activity (and all other activites) o2 is always null (although set with the service). For the service o1 is always null (although set in main) :( But if I understood Java-Objects correctly this should not be the case, am I right?

Any ideas where the problem might be?


The problem is not with Java but with Android. Android doesn't allow you to access variables of a different process. Just remove the android:process attribute from the service in the manifest file So that the service runs in the same process in which your activity is running.


you can add modifier static to o1,o2 from service class , but you can use directly the ones from main why do you want to make copy? Using static objects in android helps to cache and is good to keep them just in one class


The reason this is failing is because you're setting this.o2 to a new reference to an object. In this program you have four references and two objects.

main.o1 -> OBJECT1
main.o2 -> NULL

At beginning of the program.

On line this.o2 = main.o2, you are setting this.o2 as a reference to main.o2. So now you have:

this.o2 -> main.o2 -> NULL

On line this.o2 = new myObject("Something");, this.o2 is now referencing a new object all-together. This gives you:

this.o2 -> OBJECT2
main.o2 -> NULL

Change the last line to main.o2 -> new myObject("Something"); you'll get:

this.o2 -> main.o2 -> OBJECT2

Please note, this is all considered bad coding practice. You never want to change a static variable from a class. Also, in Android, you generally don't want to access the call of an activity directly like this.


As DeeV says, using statics this way is not great practice but if you absolutely have to do it then putting them in your Application class contains the mess to some extent.

Classes derived from Context (e.g. Service and Activity) support the getApplication() or getApplicationContext() method which you have to cast to your application class to access the variables.

public class MyApplication extends Application {
       public static myObject o1; 
       public static myObject o2; 
}

public class MyActivity extends Activity {
       public void onCreate(Bundle state) {
              myObject o1 = ((MyApplication) getApplication()).o1;
              }
}

public class MyService extends Service {
       public void onCreate(Bundle state) {
              myObject o2 = ((MyApplication) getApplication()).o2;
              }
}

Bear in mind that you will have to synchronize access to the variables from different threads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜