Passing variable from main to Async class

Posted by Bigflow on Stack Overflow See other posts from Stack Overflow or by Bigflow
Published on 2012-11-19T09:58:20Z Indexed on 2012/11/19 11:05 UTC
Read the original article Hit count: 219

Somehow, I can't get this done. This is all what I tried till now:

  1. Main:

    private String myState;
    
    public String getState() {
        return myState;
    }
    
    public void setState(String s) {
        myState = s;
    }
    

    Async:

    Main appState = ((Main)getApplicationContext());
    String state = appState.getState();
    

    Error: No enclosing instance of the type Main is accessible in scope

  2. Tried with Helper(Globals) class.

    public class Globals extends Application{
          private String test= "1";
    }
    

    Main:

    private Globals mGlobals;
    mGlobals = new Globals();  
    mGlobals.test = "2";
    //Do Async thing
    

    Async:

    private Globals mGlobals;
    mGlobals = new Globals();
    print mGlobals.test;
    // (result is 1, should be 2)
    
  3. Also something else, but don't remember good.
    Tried alot of things (backspace and del buttons are over-used :p )
    But I can't get everything working.

    Async class doesn't have an activity.

Code pasted: http://pastebin.com/ikcsdL1p

© Stack Overflow or respective owner

Related posts about android

Related posts about android-asynctask