So I have an array and is displayed in IntentA (using listview). Then I click on an item in IntentA to navigate to IntentB. In intentB I modify the array that is used in IntentA. When I go back, the array is not updated.
Is there a way to solve this issue?
Here is the code
Intent A: Intent i = new Intent("com.my.package.MYBOOKMARKS");
startActivity(i);
String BookmarkList[] = Utils.getBookmarksArray();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View logo = inflater.inflate(R.layout.bookmarks,
(ViewGroup) findViewById(R.id.logo));
lv.addHeaderView(logo, null, false);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.menu, BookmarkList));
Intent B: @Override
public void onClick(View v){
if (BookmarkButton.isChecked()) {
Toast.makeText(AClass.this, "Added to bookmarks", Toast.LENGTH_SHORT).show();
Utils.setBookmarksArray("AValue");
checked = true;
} else {
Utils.removeFromBookmarksArray("AValue");
Toast.makeText(Aclass.this, "Removed from bookmarks", Toast.LENGTH_SHORT).show();
checked = false;
}
}
Thank you.
If you are using an ArrayAdapter to display your items in the listview then you may need to call adapter.notifyDataSetChanged() after modifying the data in IntentB or similarly just find a way to invalidate the data in the listview so that it is redrawn.
If the above does not solve your issue then yes you may need to be saving and restoring your data per the Android application life cycle as Tim suggested. Also please post your code.
Update since code posted: So it looks like you have helper methods in a Utils class that handle adding and removing the bookmarks from the ArrayAdapter. As I mentioned before you should add the line adapter.notifyDataSetChanged() after you add and remove items from the Array.
Also when going back to IntentA it is possible that you are re-initializing the activity and so setting a new ArrayAdapter to the list and therefore clearing it each time. I would implement the methods onSaveInstanceState and onRestoreInstanceState in activity A so that the data in the Array is persisted when it leaves focus.