-2

I am getting stuck to clear the preference. I created one method to use for all activities.

 public void clearMyPrefrences() {
        SharedPreferences.Editor editor = preference.edit();
        editor.clear();
        editor.commit();
    }

But In my FirstActivity there is some EditText on which I am opening new activity and searching some contents.On the click of EditText it will open SecondActivity. When I select some option from searchmenu and back to my FirstActivity it clears data before I typed. I am calling this method on the onStop().I want to clear this data when I go back from the FirstActivity.So how should I do this??

How to clear data of preferences If I have two activites - In FirstActivity -

there are some EditText. On the click on the EditText -

SecondActivity opens. In the SecondActivity I search some data and set the selected text in the EditText of FirstActivity.

But problem is when I search and set the Text of selected item It clears all the data of FirstActivity which I typed in my activity.

And When I call above method on the onStop() it is not working.

Please tell me if there is the solution for it.Thank you.

Asteroid
  • 718
  • 7
  • 21

1 Answers1

0

You need to call clearMyPreferences in onDestroy() instead of onStop () to achieve your desired result.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • I tried that,its not working.It clear data of FirstActivity when I come from SecondActivity to FirstActivity. I want to clear the data on the backpress of FirstActivity.@Sagar – Asteroid Apr 06 '18 at 11:16
  • How are u saving the data? You need to ensure you are not calling `finish()` when you start Second Activity. – Sagar Apr 06 '18 at 11:25
  • I am saving data in shared preferences and I am not calling finish() when I start Second Activity. – Asteroid Apr 06 '18 at 11:29
  • Can you update your question with the code for saving? And starting Second Activity? – Sagar Apr 06 '18 at 11:33