1

This question has been asked before but I didnt find the answer helpful to my specific case.

I'm populating the ArrayList parts from another activity the first time I access the class but I keep losing the parts instance when I try to access it after the initial creation of it. Every time Task is called parts is empty and I'm not sure how to keep it filled with the details I want it filled with.

public class Task extends ListActivity{
    private PartAdapter partsArrayAdapter;
    ArrayList<Parts> parts ;//= new ArrayList<>()

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null){
            parts = (ArrayList<Parts>)savedInstanceState.getSerializable("parts");
        }
        else {
            try {
            // Get the Bundle Object
            Bundle bundleObject = getIntent().getExtras();

            // Get ArrayList Bundle
            parts = (ArrayList<Parts>) bundleObject.getSerializable("parts");


            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        setContentView(R.layout.activity_parts_list);

        partsArrayAdapter = new PartAdapter(this,parts);
        setListAdapter(partsArrayAdapter);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        outState.putSerializable("parts", parts);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        parts = (ArrayList<Parts>)savedInstanceState.getSerializable("parts");
    }
}
Pankaj
  • 7,908
  • 6
  • 42
  • 65
  • 1
    Really? https://stackoverflow.com/questions/5374546/passing-arraylist-through-intent – shkschneider Jul 03 '15 at 08:17
  • are you trying to restore values from the bundle after an application restart or after a configuration change? – eduyayo Jul 03 '15 at 08:22
  • You are using Serializable to store your list in the Bundle. I recall it is not recommended. If possible, have your Parts class implement Parcelable and then store it in the Bundle using setParcelableArrayList(). – JDenais Jul 03 '15 at 08:46
  • eduyayo im not sure i only started android programming last week. If i was allowed to use a constructor for Task i would initialize parts as an instance variable for each task object so that I could access parts array. What is happening however is im not storing the parts array properly and when i access it via another activity(?) it crashes. It crashes because i havent initialised parts but i already have. I'm sorry if this is poorly worded but like i said i kinda dove into this headfirst and im not sure quite whats going on. – kwajo mensah Jul 03 '15 at 10:20
  • shkschneider my issue isnt with passing an arraylist from from one intent to another its with keeping the arraylist from dissapearing once its moved over. The try block i have is taking arraylist i made in another activity and storing it in parts. If im missing something and what you suggested will solve my problem can you please give me a more elaborate explanation because "Really?" isnt going to cut it... also i know it might be obvious for you what to search for but as someone who is still fairly unfamiliar with the jargon its very easy to overlook solutions. – kwajo mensah Jul 03 '15 at 11:04
  • Have a look at this. http://scriptedpapers.com/2013/11/26/android-pass-arraylist-of-custom-class-via-intent/ – Shrinivas Shukla Jul 05 '15 at 10:55

0 Answers0