0

Right now I am retrieving the images and texts stored in Server using Web Services. I am consuming it by using SOAP. But the thing is that every time I am opening the app, it starts loading again & again. It is because in oncreate method of an Activity I have called the asynctask, which is loading the datas in background.

I want to implement in the order that, first I will retrieve the datas from webservice and then will display them in my app. Now if I am closing my app and opening it once again or if at that time internet is not connected, it should display the datas which were loaded during last session. How to acheive this???? Please share your ideas

Thanks & Advance

ARIJIT
  • 515
  • 8
  • 22

5 Answers5

1

You can cache the bitmaps from a remote service using a Disk Cache, there is more information about how to do this on the Google developer site http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

This will allow you to store the images and display immediately if there is no connection, or you want to load the images whilst loading the remote images.

Depending on what text you need to store you can associate the text with the images in the cache or alternatively set up an ArrayList with the data and store to disk. Some more details here Best Way to Cache Data in Android

Lastly, there are tools around to ensure you are making the most of your network code, such as the AT&T ARO tool, running this will help you to optimize your app by reducing your network calls to a minimum. See http://developer.att.com/aro

Community
  • 1
  • 1
Rod Burns
  • 2,104
  • 13
  • 24
0

If you want to implement something like that.. you should create a DB for you application so you data wont be deleted when you close your application.

because with every other option your data maybe deleted by the GC if your app is not the active application.

then you should create a service that would run every specified amount of time and will check for internet connection, and if it find's it to check if there is new data in the server.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
0

I think that the same behaviour will occur if you change the orientation of the device. It's because when re-creating your application, the same method is invoked. You should set a flag inside the function which load the images and then, in onCreate() and onResume() methods you should check if the flag is set and stop the function from being called. See if that helps.

Cheers

g00dy
  • 6,752
  • 2
  • 30
  • 43
0

If you would like to "re-use" those image again, you may consider caching them into devices SD card. For example:

FileOutputStream outFile = new FileOutputStream(path);
image.compress(Bitmap.CompressFormat.JPEG, 100, outFile);
outFile.close(); 

And then receive the file from sd card and decode that by BitmapFactory probably in your activities' onResume() method by certain logic (for example, to detect whether the device is connected to Internet, etc.):

File imageFile = new File(path);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

Remember to add necessary permission to access SD card :)

hungr
  • 2,086
  • 1
  • 20
  • 33
0

Android implements the MVC (Model-View-Controller) pattern, where you just reuse (in most cases) the existing View classes and implement your Controller and Model. Your Activity is a Controller. But such operations as downloading data should be done by the Model.

So you have to create a class for data that will survive Activity destruction and creation (which, in particular, happens when the orientation is changed).

Invoking an AsyncTask from an Activity is just plain wrong.

In addition, keep in mind that under some circumstances Android may kill and restart the application process, thus causing loss of any data kept in RAM. (You are guaranteed to see the onPause() notification, though.) But your application must be able to recover from such restarts. See onSaveInstanceState(Bundle) and the Bundle argument in onCreate (Bundle savedInstanceState).

Also, see: Is AsyncTask really conceptually flawed or am I just missing something?

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127