-2

In my app I want to allow the user to insert some data which later appears in a list. This aspect is organized as follows: From the Main Activity, the user can press a button which opens the Editor Activity. Here it's possible to construct the data. Pressing the confirm button, the built data are showed in another activity (New Element Activity), in a list. Well, if the user goes back through the designated button from New Element Activity to Main Activity I want the list to be cancelled, while now, when adding a new element, the former is still there (that's because the array with the data was initialized before onCreate() method - I don't want to change it). I tried to override the onBackPressed() method, but it didn't work. Suggestions?

Here's my code - the onBackPressed() method.

 @Override
public void onBackPressed(){
    super.onBackPressed();
    data.clear();
    Log.d("onBackPressed", "called");
}

Clicking the back button just returns to the previous activity. The overridden method doesn't get called. Thanks in advance

paola91
  • 339
  • 1
  • 5
  • 14
  • I recommend you read **[how to ask perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)**, next **[how to create Minimal, Complete and Verifable example](https://stackoverflow.com/help/mcve)** and quickly edit your question accordingly before it gets downvoted and burried. – Marcin Orlowski Aug 16 '17 at 17:48
  • What does it mean "it didn't work"? Be more specific. Also, provide source code of the relevant parts that don't work and as much **relevant** information as possible (for example, logs). – Xavier Rubio Jansana Aug 16 '17 at 18:19
  • Ok, I just edited my post. – paola91 Aug 16 '17 at 19:22

1 Answers1

0
 @Override
public void onBackPressed() {

    startActivity(new Intent(CurrentActivity.this,NextActivity.class));

}
Jeffy
  • 276
  • 3
  • 9