Anything I've written so far has only been good for one instance of the activity in my app. The last course on the Treehouse Android track was about storing information. Every time another activity is stacked on a previous activity, the previous activity is paused and Android may stop and destroy some of the data to free memory. However there are two methods that can be overwritten
OnSaveInstanceState
OnRestoreInstanceState
OnSaveInstanceState bundles information that should be saved. To retrieve that information the method OnRestoreInstanceState would be implemented and in that method you would have to write the code to set the variables in the bundle to its respective member variable. This is absolutely necessary because anytime the screen orientation would change while using the app, android essentially stops and restarts the activity to display it in the correct orientation. It's interesting because in the onCreate method, Android Studio puts in a parameter for a savedInstanceState Bundle. So it will already expect to load previous information.
There is also a data type of SharedPreferences. I still need to experiment with this type, and I have some ideas for my first app I'm writing to try it out. A variable can also be assigned to a SharedPreferences.Editor. This can thus implement edit, putInt/String/etc., and apply. They are basically self descriptive. I may be slightly wrong in my current understanding, but this is how I understand it at present. For example a member variable mSharedPreferences is set to a constant that is a file location in the package. The string would be the package path plus ".preferences". Another member variable mEditor can then be set to mSharedPreferences.edit(). Then you can apply functions to mEditor, such as putInt/String/etc.(key, value). Then after all variables are "put" into the editor, you call the apply method to save it. The location the information is saved in the place initially set up in the mSharedPreferences variable. To live update a preferences edit, you would call notifyDataSetChanged() on say, a list adapter.
I hope I can reread this later and understand it. And if anyone is reading this, I hope it makes sense. If the last paragraph makes absolutely no sense, especially after trying it, I would appreciate a way to explain it better. The more I use these functions the more I will understand them. I'm still unsure of how to implement adapters for list views as I am currently still just copying and pasting code while adapting the variables to match each activity.
So far everything I've ever learned in programming, from last years Python and PHP/SQL to the current Java and Android courses, has been an amazing experience and I feel I've come so far. I'm really looking forward to what more I can do in the coming months. I think too that I should document how many man-hours I am investing in writing this. Then I can look back and know how much time it took to write my first app.
The constant earwigs of how to figure things out however is likely incalculable.
No comments:
Post a Comment