Issue
My application has two layouts A and B.
In A a user chooses some data by pressing RADIO BUTTONS. Then he presses a BUTTON to go to the B layout and use this selected variables to dynamically build some views (EditTexts, TextVies, and Buttons).
In layout A i implemented onDestroy() (same thing happens if i implement onStop()) to save user selected data. If i exit the app by back or by pressing down home and removing it from task manager, the user selected data will still be visible if the user starts the application again.
Now, in layout B, first i build first part of views based on layout A's user selected data, then i also have extra Buttons that allow a user to add and remove as many view to that layout as he/she needs.
NOW HERE COMES MY CONFUSION................... After the user fills out whatever is needed in layout B, then goes back to A, or exits the app, I WANT TO SAVE THAT DATA ENTERED IN LAYOUT B. I know how to save the data using SharedPreferences BUT i do not know which methods i need to implement for layout B or maybe even additionally for A to accomplish my mission.
THIS IS EXACTLY WHAT I WANT TO HAPPEN..... All i want is to be able to start the app with user's filled data for layout B. Layout A seems to be working. IF USER SELECTS SAME DATA IN LAYOUT A (meaning leaving everything the way it is), only then i want to use saved data, otherwise i want to restart new layout B with user's new selections.
One more problem. If user goes back to layout A from B, since everything was saved in layout A already he can press a button and come back to layout B (maybe wants to change his/her selected values or just accidentally pressed back button). I implemented onDestroy() for layout B as well but i have a problem with it. If user does not change the selections in layout A and goes to layout B again, everything that i saved is rebuilt now, but if he/she goes back to A and comes to B again, it is not there, this keeps happening every time... If i go back and forth i will see the saved data only on odd occasions, on even occasions it is not there.
In layout B i saved everything in onDestroy(), then onCreate() i check whether layout A's data is the same so i can rebuild the layout B again; otherwise, layout B will be new.
Solution
Instead of using onDestroy()/onCreate()
you'd better use onPause()/onResume()
, because these are always called, but a call to onDestroy() might be missed here and there.
You would need to implement onPause()
in both your Activities, A and B.
Once you've figured out how to reliably save your preferences in onPause()
, you may proceed with the discarding saved data when you don't need it according to your application logic.
Answered By - lenik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.