GOAL: I need to get a value within an async task. The value is declared within a previous called Activity.

QUESTION: is a bad practice to instantiate the Activity from which I need the value just to get it using a proper getter? Or is it wrong (I mean against Android architecture good practices) ?


 public class MapsActivity extends AppCompatActivity {

       private static int foo;

       public int getFoo(){ return this.foo }



public class doAsync extends AsyncTask {

    protected void onPostExecute(Void... params) {

    MapsActivity maps = new MapsActivity();
    int foo = maps.getFoo();

  • How is maps related to the other instance of MapsActivity? This looks like reading an uninitialised property – Caleth 2 days ago
  • You are right. It's uninitialised. I tried to make the variable static to access it by MapsActivity.Foo but I got error about referencing a static fiedl from a non-static context. – Maicake 2 days ago
  • It sounds like you should probably pass the existing instance of MapsActivity to the constructor of doAsync, such that it is available in the body of onPostExecute – Caleth 2 days ago
  • @Caleth I correct myself. It's not uninitialised because I delcared it static so that means that for every instance of MapsActivity there will be just one instance of foo for all the objects. – Maicake 2 days ago
  • 1
    Yes, this is bad, for the reasons explained in Why is Global State so Evil? – Caleth 2 days ago

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.