UPDATE: I decided not to use setRetainInstance(true) at all and I solved the problem using the ObjectInputStream and ObjectOutputStream classes and saved the arrHashMap object to a file in the onSaveInstanceState(Bundle outState) method of MyActivity, and retrieved the arrHashMap object from that file in the onRestoreInstanceState(Bundle savedInstanceState) method of MyActivity. I then proceeded to set the adapter with the retrived arrHashMap object.
As an added note, I changed the arrHashMap instance variable of MyFragment to a static variable so it could be accessed from MyActivity.
Save Code:
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
try
{
File f = new File( this.getDir("myDir", Context.MODE_PRIVATE), "arrHashMap");
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(f));
os.writeObject(MyFragment.arrHashMap);
os.flush();
os.close();
}
catch(IOException e)
{
return;
}
}
Restore Code:
@Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
ArrayList<HashMap<String,String>> arrHashMap;
try
{
File f = new File( this.getDir("myDir", Context.MODE_PRIVATE), "arrHashMap");
ObjectInputStream is = new ObjectInputStream(new FileInputStream(f));
arrHashMap = ((ArrayList<HashMap<String, String>>) is.readObject() );
is.close();
}
catch (Exception e)
{
arrHashMap = null;
}
if(arrHashMap != null)
{
ListView lv = (ListView)findViewById(R.id.fragment_lv);
CustomListViewAdapter adapter = new CustomListViewAdapter(this, arrHashMap);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
}