Question

I'm writing an AIR Native Extension that has to do some work on a new Android Thread. I have to use a Context to get device information in this Thread. I am calling the getActivity method of FREContext in a new thread, which segfaults.

In my FREContext-extended class, I have:

public class MyExtensionContext extends FREContext {    
    public void doThreadWork() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // calling com.adobe.fre.FREContext.getActivity(), crashes here
                Activity act = getActivity(); 
            }
        }
    }
}

According to the Adobe documentation, FREObjects are not allowed to be accessed outside the FREFunction thread that's running. Is there a similar case with FREContext and new threads?

Was it helpful?

Solution

Ok, it turns out I cannot call getActivity() directly from FREContext. I had to set it via my FREFunction:

public class MyFunction implements FREFunction {

    @Override
    public FREObject call(FREContext context, FREObject[] passedArgs) {      
        MyExtensionContext mec = (MyExtensionContext) context;
        // Set a local variable in MyExtensionContext to getActivity value
        mec.act = mec.getActivity();
        mec.doThreadWork();
        return null;
    }
}

And my FREContext now looks like this:

public class MyExtensionContext extends FREContext {    
    public Activity act;        

    public void doThreadWork() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // Do stuff with Activity "act" here
            }
        }
    }
}

I'm not entirely sure why this works since it seems like an extra step that's getting the same getActivity() value, but it could be that this Activity is created at a different time than in FREContext.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top