0

I have an activity group and it starts 2 activities. When the user presses a button on one of the activities, the activity group populates an ArrayList.

I am wondering if there is a way to allow both of my activities to access this ArrayList.

Here's what I have at the moment:

public class ExampleGroup extends ActivityGroup {

    public static ExampleGroup group;
    ArrayList<String> strs = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        group = this;

        View exampleView = getLocalActivityManager().startActivity(
            "Example",
            new Intent(this, Example.class).addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();

        setContentView(exampleView);

    }

    public void populateArrayList(){
    //code to do it
    }

}

public class Example extends Activity {

   @Override
    public void onCreate(Bundle savedInstanceState) {

        ExampleGroup.group.populateArrayList();

        ArrayList<String> strs2 = ExampleGroup.group.strs;

        Log.i("ArrayList contents", strs2);

    }

}

The arraylist returns null. Is there something I am missing, or is there a better way to do it?

NotACleverMan
  • 12,107
  • 12
  • 47
  • 67

1 Answers1

2

Yes essentially you're wanting to share a model object between two activities, and this has much to do with the structure of your program. See this post for more details on how that can be done:

Where should I put global methods and variables in an Android app?

Community
  • 1
  • 1
chubbsondubs
  • 37,646
  • 24
  • 106
  • 138