0

I have an app where I save the values chosen from a spinner into a list view. The problem is, when you turn off the device, or remove the app from multitasking, the list view becomes blank when you run the app again. However, when you run the app from multitasking, the values are still there and they don't disappear. My code:

ListActivity.java

package viva.inspection.com.inspectionpicker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import viva.inspection.com.inspectionpicker.R;

public class ListActivity extends Activity {
    private static ArrayList<String> inspections = new ArrayList<String>();
    private static ArrayAdapter<String> inspectionAdapter;
    String value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ListView inspectionList = (ListView) findViewById(R.id.listView);
        inspectionAdapter  = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, inspections);
        inspectionList.setAdapter(inspectionAdapter);
        if(getIntent().getStringExtra("spins") != null) {
            value = getIntent().getStringExtra("spins");
        }
        if(value != null) {
            addItems(value);
        }
    }

    public static void addItems(String s) {
        inspections.add(s);
        inspectionAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch(id) {
            case R.id.action_settings:
                return true;
            case R.id.action_new:
                startActivity(new Intent(this, MyActivity.class));
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

EDIT: I saw this question asked earlier that sort of solves my problem. I don't have an arraylist of custom objects, however, it's just an arraylist of strings, so how do I implement the Parcelable interface.

EDIT #2: I have solved my problem using this code:

protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList("inspection list", inspections);
}

Then I modified the onCreate method:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ListView inspectionList = (ListView) findViewById(R.id.listView);
        inspectionAdapter  = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, inspections);
        inspectionList.setAdapter(inspectionAdapter);
        if(getIntent().getStringExtra("spins") != null) {
            value = getIntent().getStringExtra("spins");
        }
        if(value != null) {
            addItems(value);
        }
        if(savedInstanceState != null ) {
            //Then the application is being reloaded
            inspections = savedInstanceState.getStringArrayList("inspection list");
        }
    }

Thank you to those who helped. I appreciate it.

Community
  • 1
  • 1
Vishwa Iyer
  • 841
  • 5
  • 14
  • 33
  • Have a look at this: http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29 – Henry Jul 11 '14 at 20:12

1 Answers1

0

I'm guessing that you're launching ListActivity from an activity in your application from the fact you're reading extras from in a intent (if not feel free to correct me). The reason it works when you launch from multi-tasking is because normally the activity state is started in onCreate() persists until onDestroy() is called. In your case , this means that if you open an app from multitasking, the application probably wasn't killed to free memory, so it picks up where you left off.

If you're re-opening the application after removing it from multi-tasking and don't reselect the items from your spinner, then the intent won't have any extras, so you'll end up with an empty list.

You'll probably want to look into persisting the data in onPause() or onSaveInstanceState() if you want the application to save the state across runs. See here for options of saving the data; what you choose will probably depend on what kind of data you're trying to save.

z153
  • 156
  • 5
  • The ListActivity is the main activity, and it's the first one that launches, and yes I'm reading extras from an intent. – Vishwa Iyer Jul 12 '14 at 09:53