I've tried a few dozen tutorials and guides off stackoverflow and other sites to try and get this to work right. The app in question has a custom class that holds a list, with items that have a list within them. Ideally I'd like to be able to save from other activities when they close or are paused. The issue I run into is that when I close another activity (by pressing the back button), it shows a log of the save message. But when I open it back up instead of loading the info it should have saved it shows an blank list. Here's my code:
This is the main activity where the custom class is referenced and the save and load methods can be called:
public class ReminderList extends AppCompatActivity {
public static ListHolder Lholder;
public static ArrayList<ReminderType> AllReminders=new ArrayList<>();
public reminderCAdapter adapter;
public static ReminderType currentReminderType;
public static ItemType currentItemType;
public static TimeType currentTimesType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder_list);
//generate list
File file=new File("f.rem");
if(file.exists()) {
try {
LoadData();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
AllReminders=Lholder.Rlist;
}
adapter=new reminderCAdapter(AllReminders,this);
ReminderType r1=new ReminderType("Thing1");
//AllReminders.add(r1);
ReminderType r2=new ReminderType("Thing2");
//AllReminders.add(r2);
//instantiate custom adapter
//MyCustomAdapter adapter = new MyCustomAdapter(AllReminders, this);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(R.id.listView);
lView.setAdapter(adapter);
}
public void onResume(){
super.onResume();
adapter.notifyDataSetChanged();
}
public static void SaveData() throws IOException {
Log.e("Saving Data", "trying");
FileOutputStream fos=new FileOutputStream("f.rem");
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(Lholder);
oos.close();
}
public void LoadData() throws IOException, ClassNotFoundException {
Log.e("Loading Data", "trying");
FileInputStream fis=new FileInputStream("f.rem");
ObjectInputStream ois=new ObjectInputStream(fis);
Lholder=(ListHolder) ois.readObject();
ois.close();
}
public void AddToList(View view){
Intent intent = new Intent(this,ReminderSettings.class);
intent.putExtra("Type", "new");
startActivity(intent);
}
}
The other activity calls the save function like so:
@Override
protected void onStop() {
super.onStop(); // Always call the superclass method first
try {
ReminderList.SaveData();
} catch (IOException e) {
e.printStackTrace();
}
}
Here's a little list of the most recent posts Ive tried:
How to create a file on Android Internal Storage?
If anyone can help me understand why this isn't working or what I need to do, I would greatly appreciate it.
Thanks much