0

I have an array list with a list of 100 records which I fetch during the availability of the internet. When the internet is not available then I am fetching it from the Application Class instance. Here is the method how I am doing it :

At the parsing step(when internet is available, after parsing the data) :

BApplication.bAppSession.setFindBData(findBDataList);

Here is the Application Class:

public class BApplication extends Application {
    public static BAppSession bAppSession;

    @Override
    public void onCreate() {
        bAppSession = new BAppSession(getApplicationContext());
    }
}

Here is my Session Class:

public class BAppSession {

    private Context context;

    private ArrayList<FindMyBeerData> findMyBeerDataLast = new ArrayList<FindMyBeerData>();
    public BeerAppSession(Context context) {
        this.context = context;
    }

    public void setFindMyBeerData(ArrayList<FindMyBeerData> findMyBeerDataList){
        this.findMyBeerDataLast = findMyBeerDataList;
    }

    public ArrayList<FindMyBeerData> getFindMyBeerData(){
        return this.findMyBeerDataLast;
    }
}

How to Fetch the Data(Offline):

findMyBDataList = BApplication.bAppSession .getFindMyBeerData();

Problem : Everything is working fine, but the problem is when I kill the application from the task manager and restart again (when the internet is not available) , I am getting empty ArrayList. DATA IS LOST WHEN APPLICATION IS KILLED

Query: How to get the data back even if the application is killed by the user ? Suggest me any way to do the same.

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143

1 Answers1

2

From this answer - https://stackoverflow.com/a/19335539/816416 - it's clear that there's no event called when an application is killed from the task manager or killed by the system itself.

So, the better way is to save the data whenever you fetch it. This will ensure that your data is safe whether your app is killed or not. Next time when you log in, you can load the saved data.

For storing data, you can use the following:

If you have an Activity, you can save your data inside the onDestroy() call.

Community
  • 1
  • 1
Vishnu Haridas
  • 7,355
  • 3
  • 28
  • 43
  • I have an ArrayList Can I Save it ? – Gaurav Arora Nov 18 '13 at 04:19
  • 1
    @GauravArora Sure, you can serialize the object and write as a string. Then you read back the string, and initialize the object. You can see a set of nice examples here - http://stackoverflow.com/questions/16111496/java-how-can-i-write-my-arraylist-to-a-file-and-read-load-that-file-to-the – Vishnu Haridas Nov 18 '13 at 06:04