title: Using Android back button after delete an item with SwipeDismissListViewTouchListener
body: <p>I have a problem when using the back-button in my app (API 17). There are two activities in my project:
  The main activity with a ListView reading items from a xml-file.</p>
  
  <p>A second activity which is reading the same xml-file but using only the items which has been checked and saved by the main activity. </p>
  
  <p>If I remove an item from the ListView in the second activity and use the app's menu to go back to the main-activity after saving the xml-file when swiping, this item is now unchecked. It's because the xml-file has been altered after swiping and the value for "isDone" was set from true to false. </p>
  
  <p>So far so good!</p>
  
  <p>But if I go back using the Android back-button the deleted item has is still checked.<br>
  What went wrong?
  I guess it has something to do with the lifecycle of the activities. But I can not figure it out. Do I have to read the altered and saved xml-file in onResume again? I assumed it will be read again when the main-activity starts after using the back-button from the second activity.</p>
  
  <p>And what I really do not understand is this:
  When I debug the code I can see that the xml-file was not altered after swipping the item in the second activity and save the changed isDone value to the xml. This seems a little bit strange to me. </p>
  
  <p>Any help would be fine!  </p>
  
  <p>Main-Activity:</p>
  
  <pre><code>    package com.wbapps.wbshoppinglist;
  
  /**
   * Created by Andreas on 9/4/2017.
   */
  
  import android.support.v7.app.ActionBar;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Environment;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.ContextMenu;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import org.xmlpull.v1.XmlPullParserException;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public class MainActivity extends AppCompatActivity {
      /*wb, 19Sep,2017: not yet
      public static final int REQUEST_ID = 1;
       */
      ImageButton imgbtnAddItem;
      Button btn_create_shopping_list;
      EditText input;
  
      //object for the ListView from activity_main.xml
      ListView list_view;
  
      List&lt;Task&gt; tasks;
      TodoListAdapter adapter;
      XmlParser parser;
      File file;
      ActionBar actionBar;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          file = new File(Environment.getExternalStorageDirectory(), "tasks.xml");
          parser = new XmlParser();
          tasks = new ArrayList&lt;Task&gt;();
  
          //file.delete();
          if (file.exists()) {
              try {
                  tasks = parser.read(file);
                  if (tasks.isEmpty()) {
                      file.delete();
                      file.createNewFile();
                  }else {sortList();}
              } catch (XmlPullParserException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              } catch (FileNotFoundException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              } catch (IOException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              }
          } else {
              try {
                  file.createNewFile();
              } catch (IOException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
  
          //Initialize the variables
          imgbtnAddItem = (ImageButton) findViewById(R.id.add_task_button);
          btn_create_shopping_list = (Button) findViewById(R.id.create_shopping_list);
          /* findViewById(R.id.list_view).requestFocus(); */
          input = (EditText) findViewById(R.id.input_task);
          /* id list_view identifies the listview from the activity_main.xmll */
          list_view = (ListView) findViewById(R.id.list_view);
          registerForContextMenu(list_view);
  
          //tasks = new ArrayList&lt;Task&gt;();
          adapter = new TodoListAdapter(tasks, this);
          list_view.setAdapter(adapter);
  
          //To swipe away an litem from the list
          SwipeDismissListViewTouchListener touchListener =
                  new SwipeDismissListViewTouchListener(
                          list_view,
                          new SwipeDismissListViewTouchListener.DismissCallbacks() {
                              @Override
                              public boolean canDismiss(int position) {
                                  return true;
                              }
  
                              @Override
                              public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                                  for (int position : reverseSortedPositions) {
                                      tasks.remove(position);
                                      adapter.notifyDataSetChanged();
                                  }
                              }
                          });
          list_view.setOnTouchListener(touchListener);
  
          //set the onClickListener for the button
          imgbtnAddItem.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  if (input.getText().length() &gt; 0) {
                      /* wb, 16Sep2017: look if the new item is already in the list */
                      Boolean itemfound = false;
                      for (int i = 0; i&lt;tasks.size(); i++){
                          if (tasks.get(i).getTaskContent().equalsIgnoreCase(input.getText().toString())){
                              itemfound = true;
                              AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                              builder.setMessage("This item is already in the list!");
                              AlertDialog dialog = builder.create();
                              dialog.show();
                              break;
                          }
                      }
  
                      if (itemfound == false){
                          tasks.add(new Task(input.getText().toString(), false));
                          adapter.notifyDataSetChanged();
                          sortList();
                          input.setText("");
                      }
  
                  }
              }
          });
  
          btn_create_shopping_list.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  //startActivity(new Intent(MainActivity.this, ShoppingListActivity.class));
                  Boolean selected = false;
                  Task taskTmp;
                  for (int i = 0; i &lt; tasks.size(); i++) {
                      taskTmp = tasks.get(i);
                      if (taskTmp.isDone()) {
                          selected = true;
                          break;
                      }
                  }
  
                  if (selected) {
                      Intent intent = new Intent(getApplicationContext(), ShoppingListActivity.class);
                      startActivity(intent);
                      /* startActivityForResult(intent, REQUEST_ID); */
                  } else {
                      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                      builder.setMessage("No items for shopping list selected!");
                      AlertDialog dialog = builder.create();
                      dialog.show();
                  }
              }
          });
  
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          //wb, Sep 13, 2017:
          //before doing something else - like calling a new activity - write the altered list to
          //the data sourc file tasks.xml file
          try {
              parser.write(tasks, file);
          } catch (IOException ex) {
              Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          /* Do I have to do the same here like in onCreate? It does not work yet!*/
          if (file.exists()) {
              try {
                  tasks = parser.read(file);
                  if (tasks.isEmpty()) {
                      file.delete();
                      file.createNewFile();
                  }else {sortList();}
              } catch (XmlPullParserException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              } catch (FileNotFoundException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              } catch (IOException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              }
          } else {
              try {
                  file.createNewFile();
              } catch (IOException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.actions, menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.action_clear_all:
                  clearDonetasks();
                  this.adapter.notifyDataSetChanged();
                  break;
              case R.id.action_delete_done_tasks:
                  deleteDonetasks();
                  this.adapter.notifyDataSetChanged();
                  break;
              case R.id.action_delete_all:
                  this.tasks.clear();
                  this.adapter.notifyDataSetChanged();
                  break;
          }
          return super.onOptionsItemSelected(item);
      }
  
      //wb, 15 Sep, 2017: take out the hook from checkbox
      public void clearDonetasks() {
          for (int i = 0; i &lt; tasks.size(); i++) {
              if (tasks.get(i).isDone()) {
                  tasks.get(i).setIsDone(false);
              }
          }
      }
  
      public void deleteDonetasks() {
          for (int i = 0; i &lt; tasks.size(); i++) {
              if (tasks.get(i).isDone()) {
                  tasks.remove(i);
              }
          }
      }
  
      @Override
      public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
          super.onCreateContextMenu(menu, v, menuInfo);
  
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.context_menu, menu);
      }
  
      @Override
      public boolean onContextItemSelected(MenuItem item) {
          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
          int position = (int) info.id;
  
          switch (item.getItemId()) {
              case R.id.context_delete:
                  this.tasks.remove(position);
                  this.adapter.notifyDataSetChanged();
                  return true;
              case R.id.context_edit:
                  createEditDialog(tasks.get(position));
                  return true;
              default:
                  return super.onContextItemSelected(item);
          }
      }
  
      public void createEditDialog(final Task task) {
  
          LayoutInflater li = LayoutInflater.from(MainActivity.this);
          View dialogView = li.inflate(R.layout.edit_dialog, null);
  
          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
          alertDialogBuilder.setView(dialogView);
  
          final EditText inputText = (EditText) dialogView.findViewById(R.id.edit_dialog_input);
          inputText.setText(task.getTaskContent());
          final TextView dialogMessage = (TextView) dialogView.findViewById(R.id.edit_dialog_message);
  
          alertDialogBuilder
                  .setCancelable(true)
                  .setPositiveButton("Save",
                          new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int id) {
                                  task.setTaskContent(inputText.getText().toString());
                                  sortList();
                                  adapter.notifyDataSetChanged();
                              }
                          })
                  .setNegativeButton("Cancel",
                          new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int id) {
                                  dialog.cancel();
                              }
                          });
  
          final AlertDialog alert = alertDialogBuilder.create();
          alert.show();
      }
  
      /* wb, 18Sep2017: sort the tasks list */
      public void sortList() {
          Collections.sort(tasks, new Comparator&lt;Task&gt;() {
              @Override
              public int compare(Task content1, Task content2) {
                  /* return o1.getTaskContent().compareTo(o2.getTaskContent()); */
                  /* ignore case sentsitivity */
                  return content1.getTaskContent().compareToIgnoreCase(content2.getTaskContent());
              }
          });
      }
  
  }
  </code></pre>
  
  <p>Second Activity:</p>
  
  <pre><code>    package com.wbapps.wbshoppinglist;
  
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.os.Environment;
  import android.support.v7.app.ActionBar;
  import android.support.v7.app.AppCompatActivity;
  import android.view.ContextMenu;
  import android.view.LayoutInflater;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import org.xmlpull.v1.XmlPullParserException;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  /**
   * Created by Andreas on 9/4/2017.
   */
  
  public class ShoppingListActivity extends AppCompatActivity {
      /*wb, 19Sep2017: Not yet
      public static final String RETVAL_KEY = "RETURN STRING";
      */
      ListView shopping_list_view;
      List&lt;Task&gt; tasks, shoppingTasks;
      TodoListAdapter adapter;
      XmlParser parser;
      File file;
      ActionBar actionBar;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.shoppinglist);
  
          file = new File(Environment.getExternalStorageDirectory(), "tasks.xml");
          parser = new XmlParser();
          tasks = new ArrayList&lt;Task&gt;();
          shoppingTasks = new ArrayList&lt;Task&gt;();
          if (file.exists()) {
              try {
                  tasks = parser.read(file);
                  if (tasks.isEmpty()) {
                      Toast.makeText(this, "No Items in Shopping List", Toast.LENGTH_SHORT).show();
                      //file.delete();
                      //file.createNewFile();
                  } else {
                      /*
                      Collections.sort(tasks, new Comparator&lt;Task&gt;() {
                          @Override
                          public int compare(Task o1, Task o2) {
                              return o1.getTaskContent().compareTo(o2.getTaskContent());
                          }
                      });
                      */
                      addDonelistitems();
                  }
              } catch (XmlPullParserException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              } catch (FileNotFoundException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              } catch (IOException ex) {
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              }
          } else {
              try {
                  file.createNewFile();
              } catch (IOException ex) {
                  Toast.makeText(this, "Task File not existing!", Toast.LENGTH_SHORT).show();
                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
  
          //create an instance for the view of the lauout
          shopping_list_view = (ListView) findViewById(R.id.shoppingListView);
          registerForContextMenu(shopping_list_view);
  
          adapter = new TodoListAdapter(shoppingTasks, this);
          shopping_list_view.setAdapter(adapter);
  
          SwipeDismissListViewTouchListener touchListener =
                  new SwipeDismissListViewTouchListener(
                          shopping_list_view,
                          new SwipeDismissListViewTouchListener.DismissCallbacks() {
                              @Override
                              public boolean canDismiss(int position) {
                                  return true;
                              }
  
                              @Override
                              public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                                  for (int position : reverseSortedPositions) {
                                      //wb, 15Sep: With swipe an item uncheck the checkbox for this item in the file for the shopping items
                                      for (int i = 0; i &lt; tasks.size(); i++) {
                                          if (tasks.get(i).getTaskContent() == shoppingTasks.get(position).getTaskContent()) {
                                              tasks.get(i).setIsDone(false);
                                              try {
                                                  parser.write(tasks, file);
                                              } catch (IOException ex) {
                                                  Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
                                              }
                                          }
                                      }
                                      shoppingTasks.remove(position);
                                      adapter.notifyDataSetChanged();
                                  }
                              }
                          });
          shopping_list_view.setOnTouchListener(touchListener);
         }
  
          @Override
          public void onBackPressed() {
              /* I jaust added a toas there to see if I will reach here */
              Toast.makeText(this, "we are in onBackPressed", Toast.LENGTH_SHORT).show();
              finish();
          }
  
          public void addDonelistitems() {
  
              for (int i = 0; i &lt; tasks.size(); i++) {
                  if (tasks.get(i).isDone() == true) {
                      shoppingTasks.add(tasks.get(i));
                  }
              }
          }
  
  
      @Override
      public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
          super.onCreateContextMenu(menu, v, menuInfo);
  
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.context_menu, menu);
      }
  
      @Override
      public boolean onContextItemSelected(MenuItem item) {
          AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
          int position = (int) info.id;
  
          switch (item.getItemId()) {
              case R.id.context_delete:
                  this.tasks.remove(position);
                  this.adapter.notifyDataSetChanged();
                  return true;
              case R.id.context_edit:
                  createEditDialog(tasks.get(position));
                  return true;
              default:
                  return super.onContextItemSelected(item);
          }
      }
  
      public void createEditDialog(final Task task) {
  
          LayoutInflater li = LayoutInflater.from(ShoppingListActivity.this);
          View dialogView = li.inflate(R.layout.edit_dialog, null);
  
          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ShoppingListActivity.this);
          alertDialogBuilder.setView(dialogView);
  
          final EditText inputText = (EditText) dialogView.findViewById(R.id.edit_dialog_input);
          inputText.setText(task.getTaskContent());
          final TextView dialogMessage = (TextView) dialogView.findViewById(R.id.edit_dialog_message);
  
          alertDialogBuilder
                  .setCancelable(true)
                  .setPositiveButton("Save",
                          new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int id) {
                                  task.setTaskContent(inputText.getText().toString());
                                  adapter.notifyDataSetChanged();
                              }
                          })
                  .setNegativeButton("Cancel",
                          new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int id) {
                                  dialog.cancel();
                              }
                          });
  
          final AlertDialog alert = alertDialogBuilder.create();
          alert.show();
      }
  
  }
  </code></pre>