0

The main activity has a "To ListView" button that launches a new activity with a list view. Each time this new activity loads, it calls an AsyncTask() method that retrieves some JSON remotely, parses it, and binds the data to the list view. Then, setContentView() is called (in onPostExecute()) to show the UI. How do I preserve the list view data, or at least the data array (for rebinding) on subsequent launches of that activity so that the ASyncTask() doesn't have to be called every time?

The ASyncTask() should get called only at the beginning (and not until when the application is forcefully terminated), subsequent calls should be done manually by the user perhaps with an onClick() event. I have tried setting a boolean for that purpose, but if so how to display the previous state of the list view when the boolean is false? I have also looked into onResume() and onBackPressed() but they don't seem to be much relevant.

Main.java:

    toListView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent myIntent = new Intent(getApplicationContext(), ListView.class);
            startActivity(myIntent);
        }
    });

ListView.java:

    public class ListView extends ActionBarActivity {
private static boolean isFirstLaunch = true;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    if (isFirstLaunch) {
        // execute the ASyncTask();
        isFirstLaunch = false;
    }
    // else display previous listview data or last activity state
}// onCreate()
andzrev
  • 544
  • 1
  • 6
  • 17

1 Answers1

1

For persisting data in your Android application, you have two obvious choices.

  • Using an SQLite database. This alternative is good if you have quite a lot of data to manage, sort and maintain. E.g if your JSON response contains a lot of data, persisting it to a database would be a good solution. Then you would simply query the database instead of executing the AsyncTask repeatedly. Vogella provides an excellent tutorial on this matter.
  • Using SharedPerefences. This alternative is better if you only have a couple of variables to relate to. Storing the data in SharedPreferences relieves you of the work needed to design and implement database support for you application. See the official documentation for a reference.
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • Is it possible to just restore the previous state of the ListView instead of even saving & loading the data from a database? I just wanna hit "back" to go back to `main`, then return to the list view and all the stuff would still be there from last time. Because right now it seems the ListView activity is destroyed each time I leave it. – andzrev Mar 18 '15 at 19:05
  • Unfortunately when you destroy your activity, you will have to persist your data somehow as described in the section of [saving/restoring your Activity state](http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState). In your case, I do not think it would be too much of a problem to persist "the data array" in SharedPreferences, then load it when you resume the activity. @Chauduyphanvu – Marcus Mar 18 '15 at 19:08
  • Thanks for your help, @Marcus. I ended up rebinding the data from the adapter, and set up an external onClick listener. Nothing got destroyed - I knew the data in the adapter were still active, just that I didn't rebind so nothing was showing! – andzrev Mar 18 '15 at 20:24
  • I see. I must have misinterpreted the question. Although, if you would like the data to be persistent through out the life-cycle of your Activity, you'd have to persist the data in some way. You Activity have to be "alive" for you to be able to keep the data in your adapter. Anyway - glad it worked out. Happy coding! @Chauduyphanvu – Marcus Mar 18 '15 at 20:31