-1

I'm new to android. In my app i have an arrayList which is being filled by objects being downloaded from internet. Concurrently i need two activities to have access to the arraylist, whether it is completed or not. I know things about services but i don't have any idea about how to code this. any help? My code seems like below:

class A extends Service {
    void foo(){
    //uses a loop to get elements from internet 
    //then adds the elements to myArraylist in each loop
   }
}
class B extends Activity {
    //needs to have access to myArraylist asynchronously
}
class C extends Activity {
    //needs to have access to myArraylist asynchronously
}

By the way, as of any activities, these two activities won't call for myArrayList simultaneously. And i'm not sure about using services. Any other offer?

xvx ph
  • 69
  • 1
  • 2
  • 12
  • see here http://stackoverflow.com/questions/22984696/storing-array-list-object-in-sharedpreferences or http://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – Mohammad Tauqir Dec 29 '15 at 17:50
  • Both sharedpref and arraylists are not my concern! found nothing useful there. however thanks! – xvx ph Dec 29 '15 at 18:01

1 Answers1

0

I believe the nicest way to achieve it is by using Application class:

  1. create class, call it something like MyApplication, and create field member of your ArrayList with getter and setter, something like that:

    class MyApplication extends Application {
         ArrayList<Object> downloadedArray;
    
         public void setDownloadedArray(ArrayList<Object> downloadedArray) {
            this.downloadedArray = downloadedArray;
         } 
    
          public ArrayList<Object> getDownloadedArray() {
    
              return downloadedArray;
          }
    }
    

2 Add entry in your manifest to this class:

  <application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme">

3 Call the setter after the download completed:

 void foo(){
    //uses a loop to get elements from internet
    //then adds the elements to myArraylist in each loop
    ((MyApplication)getApplication()).setDownloadedArray(yourArray);
}

4 Now you can access this array from any Activity simply by:

        ArrayList<Object> downloadedArray = ((MyApplication)getApplication()).getDownloadedArray();

Just Don't forget to check if that ArrayList isn't null.

yshahak
  • 4,996
  • 1
  • 31
  • 37
  • Seems a nice solution. But i need to have access to the arraylist even during the time it is being filled (when elements are being downloaded and added). Is this possible then? and doesn't it cause error in result of asynchronous access to the arrayList (multiple access at the same time)? – xvx ph Dec 29 '15 at 18:34
  • So call the setter right after the Array created. About the Asynchronous, as long as you not change it not supposed to be a problem as far as I know – yshahak Dec 29 '15 at 18:51
  • I tried to implement your solution in my app but there were some major defects. Like i need the process of download stay active when user switches between activities. So this may just get done by a service. Therefore i asked a new question. You may be busy but i wonder if you take a look at it? http://stackoverflow.com/questions/34534014/download-data-from-internet-in-background-and-concurrently-share-them-among-all – xvx ph Dec 30 '15 at 17:18