0

I have a MainActivity and a second Activity which has an EditText. I want that the content of EditText always gets saved. However I don't want a EditTextChangedListener which writes a file after 1 char has changed.

What is a good solution? I thought about onPause or onBackPressed.

What about the home button? I think the app remains open, so is there any need to save? And what about other interrupts like phone calls? Does onPause catch all that?

Thank you.

NoobieNoob
  • 887
  • 2
  • 11
  • 31

3 Answers3

0

Yes onPause gets called whenever your app gets interrupted or goes to background check Activity life cycle

Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
0

A good solution is to include such logic in the onPause() method. It will always be called in all situations. This is what the developer documentation says:

you should use the onPause() method to write any persistent data (such as user edits) to storage.

One thing you should keep in mind is that this method may be called more frequently than desired, for example, when your screen light goes off (some ppl have 15sec screen light timeouts). So, you should not put in too many expensive operations inside there.

As for pressing of home button, it is recommended that you save the data (at onPause()). The reason is that your activity is in the background but it may get destroyed. The system may destroy the activity if it needs to reclaim the memory. (For example you start too any other apps afterwards and put them all in the background) From the documentation:

Stopped: The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
0

No. The correct answer here is to listen for the "return" key event. That signifies that the user has completed input to the field, and trigger the save of the field contents to the file. It's useful in many other circumstances too.

See this answer: Android Use Done button on Keyboard to click button

Peter.

Community
  • 1
  • 1
Peter vdL
  • 4,953
  • 10
  • 40
  • 60