Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Android. Sharing data between activities

Options
  • 11-11-2015 6:38pm
    #1
    Registered Users Posts: 1,606 ✭✭✭


    A little background. Firstly I have some experience coding but not too much on android.

    I have a simple android app which consists of a single activity. It collects user input, stores to an ArrayList( linked to a ListView) and saves to a file.

    I want to re-code it to comply with best practice, put file writes on a separate activities etc.
    My problem at the moment is passing data between activities. Currently i have two activities , one where user enters the data. I want a second activity to display a ListView, and another to display data graphically.

    How do I pass data between the different activities? Specifically, one activity contains a ListView and I want that updated with the ArrayList.

    I accept I am probably overlooking something very simple but can anyone point me in the right direction?
    Thanks


Comments

  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    rock22 wrote: »
    A little background. Firstly I have some experience coding but not too much on android.

    I have a simple android app which consists of a single activity. It collects user input, stores to an ArrayList( linked to a ListView) and saves to a file.

    I want to re-code it to comply with best practice, put file writes on a separate activities etc.
    My problem at the moment is passing data between activities. Currently i have two activities , one where user enters the data. I want a second activity to display a ListView, and another to display data graphically.

    How do I pass data between the different activities? Specifically, one activity contains a ListView and I want that updated with the ArrayList.

    I accept I am probably overlooking something very simple but can anyone point me in the right direction?
    Thanks

    File writes should not just go in a separate activity. Use a background thread for this, AsyncTask is the easiest but beware of the activity lifecycle getting destroyed during the save.

    You should use RecyclerView instead of ListView. ListView is more or less deprecated and RecyclerView enforces the ViewHolder pattern.

    Use intents for passing data between activities:
    Intent intent = new Intent(context, ActivityYouWantToStart.class);
    intent.putExtra(ActivityYouWantToStart.KEY, value);
    startActivity(intent);
    
    Or if you want do get a result back from the new activity:
    startActivityForResult(intent);
    

    and then use the onActivityResult() callback method to get your result.


  • Registered Users Posts: 1,606 ✭✭✭rock22


    Thanks jester77 for that reply. I will lookup RecyclerView.

    I have an activity which allows the user enter some data. Then an object if formed, based on the entered data and some set data from preferences. The object is added to an ArrayList. The user may enter data multiple times in this activity.

    When the user is finished entering the data they will exit this activity and return to a main activity. from there, they might navigate to a data view activity where they will see the records in the ArrayList. (i.e. via ListView or a recyclerView as you suggest).

    It is this ArrayList I want to share between these three activites. Is passing an ArrayList as a parameter of intent.putExtra the best way to do this?

    i had hoped to simply create the ArrayList as a App variable and access it from any activity in the App but I can't see any way of doing this. If it was created as a static array in the main activity would that work?

    Thanks again for looking


  • Registered Users Posts: 11,262 ✭✭✭✭jester77


    rock22 wrote: »
    Thanks jester77 for that reply. I will lookup RecyclerView.

    I have an activity which allows the user enter some data. Then an object if formed, based on the entered data and some set data from preferences. The object is added to an ArrayList. The user may enter data multiple times in this activity.

    When the user is finished entering the data they will exit this activity and return to a main activity. from there, they might navigate to a data view activity where they will see the records in the ArrayList. (i.e. via ListView or a recyclerView as you suggest).

    It is this ArrayList I want to share between these three activites. Is passing an ArrayList as a parameter of intent.putExtra the best way to do this?

    i had hoped to simply create the ArrayList as a App variable and access it from any activity in the App but I can't see any way of doing this. If it was created as a static array in the main activity would that work?

    Thanks again for looking

    So you have something like the following?
    List<YourModel> list = new ArrayList<>();
    

    If so, then you could have your YourModel implement Parceable, and then in the activities intent, you can use
    intent.putParcelableArrayListExtra
    


  • Registered Users Posts: 1,606 ✭✭✭rock22


    Thanks jester77,
    For the moment I am passing the data as ArrayList<String> and it does seem to be working ok. I hadn't realised it was possible to pass such as large structure with intents.

    Ignore below - my problem was just a typo .
    Thanks again for all help


    In a different area I am using startActivityForResult the codes is
    Intent intent = new Intent(this, EntryActivity.class);
    startActivityForResult(intent, entryrequestcode);

    and I am using onActivityResult(int requestCode, int resultCode, Intent data)
    to retrieve the result.
    I am using this code in the 'sending' activity to send back the result
    Intent intent = new Intent();
    intent.putStringArrayListExtra("result",recordings);
    setResult(224,intent);
    finish();


    setResult will only accept 2 parameters although internet success it should allow 3 parameters. In any event I am not getting anything back

    Just wondering if I need to register the onActivityResult method anywhere?

    Thanks again for all help


Advertisement