开发者

How to not create a list every time the activity is created

My code does this - List<HashMap<Stri开发者_如何学Pythonng, String>> painItems = new ArrayList<HashMap<String, String>>(); every time the activity is created, but I want it to persist the list throughout the application life cycle, and even when the application is closed then re-opened... Any help would be appreciated.


You are going to need to save it somewhere, SQLite DB, TextFile, XML etc. Take a look at the android activity life-cycle http://developer.android.com/reference/android/app/Activity.html and decide where you want to save and where you want to load your list.

To clarify you will need to pull the values out of your HashMap and store them in a text format using one of the methods mentioned above. Then when your app starts you have to get the values from storage and load them into the HashMap.


Answer here?

Activity Result Fix

UPDATE:

After that you will need to cache the information on the device and set it to refresh. in onClose() serialize the painItems ArrayList to a file using FileOutputStream fed by ObjectOutputStream to write your list out to a file. Use ObjectInputStream from a FileInputStream to read it back in. I recommend doing this in onPostResume() and notify the empty initialized adapter of the data set change.

UPDATE 2:

The shamelessly terse Exception handling below needs to be thought out but this should get you started.

String serfilename = "painitems.ser";

...

private ArrayList<HashMap<String, String>> loadListFromFile(
    ArrayList<HashMap<String, String>> masterlistrev) {
try {
    FileInputStream fis = openFileInput(serfilename);
    ObjectInputStream ois = new ObjectInputStream(fis);
    masterlistrev = (ArrayList<HashMap<String, String>>) ois.readObject();
} catch (Exception e) {
    e.printStackTrace();
}
return masterlistrev;
}

private void writeListToFile(ArrayList<HashMap<String, String>> masterlistrev){
    File myfile = getFileStreamPath(serfilename);
try {
    if(myfile.exists() || myfile.createNewFile()){
        FileOutputStream fos = openFileOutput(serfilename, MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(masterlistrev);
    }
} catch (Exception e) {
    e.printStackTrace();
}

}


When the activity is closed you will probably be out of luck, if it was destroyed, but, otherwise make it part of the class level properties, and check if it exists before creating it again, but doing it in the onCreate method is probably your best bet, since that is only called once/activity.

Now, if you have data there and you want that created only one time then you may want to look at something like SharedPreferences (http://developer.android.com/guide/topics/data/data-storage.html) so you can create the list, then just pull it in and display it from then on.

I did this, which is related to what you are asking. The nice part of final is that you only set it once, which is what you want. (http://www.javamex.com/tutorials/synchronization_final.shtml)

public class StartGameActivity extends GraphicsActivity implements
    final List<PaintColorCanvas> pccList = new ArrayList<PaintColorCanvas>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜