0

I'm having issues with my Application Object. I am currently using a Service to simulate incoming data from an electronic game board. This data is represented as a 2D boolean array. Every five seconds the Service uses a method of the Application Object to update the array (setDetectionMap()). This array is being read by a Thread in my main Activity using another method (getDetectionMap()). After some debugging I am almost positive that the main Activity is not seeing the changes. Here is the code for my Application Object:

 public class ChessApplication extends Application{

    private static ChessApplication singleton;
    private boolean[][] detectionMap;

    public static ChessApplication getInstance(){
        return singleton;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        singleton=this;
        detectionMap=new boolean[8][8];
    }

    public boolean[][] getDetectionMap(){
        return detectionMap;
    }
    public void setDetectionMap(boolean[][] newMap){
        detectionMap=newMap;
        Log.d("Chess Application","Board Changed");
    }
}

I've checked my Manifest, I've rewritten my object declaration a dozen times, I've added LogCat tags to make sure that the code is executing when I think it should be, and I've even implemented the supposedly redundant Singleton code. Any ideas what could be causing this? Incidentally can anyone tell me how to view variable states as the activity is running? Thanks in advance.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

1 Answers1

0

Is your Activity calling getDetectionMap() to get the new map after the update occurs?

Because otherwise, it's holding onto a reference to the old boolean[][] array, wheras setDetectionMap(...) isn't actually updating the current data structure, it's just updating the "detectionMap" variable to point to a different one. As such, your main activity won't be aware of the swapout until the next time it calls getDetectionMap.

Easy fix: in setDetectionMap, manually copy values from newMap into detectionMap. Or, update the Activity's reference so it's looking at the right map.

One other observation entirely unrelated to the original question: It's quite unusual to override Application during Android development, and is usually considered a "code smell" unless you have a really good reason for doing so. In this case I imagine it's so that you can communicate between your service and Activity, but you create a middle-man where one isn't entirely necessary. Here's a useful SO thread on how to communicate directly between the two :)

Community
  • 1
  • 1
Alexander Lucas
  • 22,171
  • 3
  • 46
  • 43
  • Thanks for the reply. So I'm actually polling for the update in my Activity using a while loop that waits until .getDetectionMap() returns a specific value (the one I'm hardcoding in my Service), but it will never acknowledge that the write has been made. However if I use the .setDetectionMap() in my main Activity to set the values before I enter this while loop, it works fine. That's why it seems to me that my Application Object is not acting as a singleton, rather that my Activity and Service are both running different instances of the Object. Thanks. – user1373705 May 04 '12 at 13:55
  • I'd be surprised if you were running two different application objects. However, just as a precaution, you could set detectionMap as static. Then if the problem goes away, you know it was two different Application instances. – Alexander Lucas May 04 '12 at 17:34
  • I tried setting detectionMap as static but the problem persists. I'm using LogCat tags to confirm that the Service and Activity are reading/writing at the expected times, and that the Application Object is responding to those calls, but yet the two seem completely unaware of each other. – user1373705 May 05 '12 at 18:59