1

I am new to Android and recently focusing on the Activity cycle. My understanding is that if we destroy an activity class, all the unsaved data will be cleared.

But whatsapp's chat page seems to be quite interesting as after I quit the chat page and re-enter that particular chat page, I would see the unsaved text remain in the edittext box.

So, how would they do the tricks? Is it related to "savedInstanceState" ?

enter image description here

4

3 回答 3

1

My understanding is that if we destroy an activity class, all the unsaved data will be cleared.

Yes unless of course important data is saved in some persistent storage in onPause() method and retrieved in onResume() method. This is common way to persist data when Activity is killed by used or OS and restarted later.

But whatsapp's chat page seems to be quite interesting as after i quit the chat page and re-enter that particular chat page, i would see the unsaved text remain in the edittext box.

This is the case when you click back button and go to the chat page again. I confirmed that this is not the case when you kill the app itself. So a good guess would be the activity is never killed and the data is persisted via savedInstanceState.

于 2015-04-03T05:28:16.627 回答
0

In normal cases, when you click Back button of your device, life span method onPause would be called. Maybe WhatsApp stores the text from the EditText into SharedPreference. When you go to this Activity again, text of this EditText would be set as the text stored into SharedPreference. Of course, in this case of WhatsApp, only its developers know.

于 2015-04-03T05:43:42.307 回答
0

With reference to the comments from Scion of Ikshvaku I created a sqlite table to store the unsent msg in edittext. It would achieve the same result as whatsapp. The trick indeed is not difficult

于 2015-04-16T06:42:32.413 回答