| Start |
|---|
| Saving and Restoring Activity State As your activity begins to stop, the system calls To save additional state information for your activity, you must implement The system will call that method before an Activity is destroyed. Then later the system will call Instance state can also be restored in the standard Activity#onCreate method but it is convenient to do it in Note that |
| Retaining Fragments In many cases, we can avoid problems when an Activity is re-created by simply using fragments. If your views and state are within a fragment, we can easily have the fragment be retained when the activity is re-created: This approach keeps the fragment from being destroyed during the activity lifecycle. They are instead retained inside the Fragment Manager. See the Android official docs for more information (opens new window). Now you can check to see if the fragment already exists by tag before creating one and the fragment will retain it's state across configuration changes. See the Handling Runtime Changes guide for more details (opens new window). |
| Manually Managing Configuration Changes If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity. However, this technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications. To take this approach, we must add the Now, when one of these configurations change, the activity does not restart but instead receives a call to See the Handling the Change (opens new window) docs. For more about which configuration changes you can handle in your activity, see the android:configChanges (opens new window) documentation and the Configuration (opens new window) class. |
Handling AsyncTask Problem:
Solution:Using Loaders (opens new window), one can easily overcome the activity destruction/recreation. Example:MainActivity:AsyncTaskLoader:Note:It is important to use either the v4 compatibility library or not, but do not use part of one and part of the other, as it will lead to compilation errors. To check you can look at the imports for |
| Saving and Restoring Fragment State Fragments also have a Then we can pull data out of this saved state in For the fragment state to be saved properly, we need to be sure that we aren't unnecessarily recreating the fragment on configuration changes. This means being careful not to reinitialize existing fragments when they already exist. Any fragments being initialized in an Activity need to be looked up by tag after a configuration change: This requires us to be careful to include a tag for lookup whenever putting a fragment into the activity within a transaction: With this simple pattern, we can properly re-use fragments and restore their state across configuration changes. |
| Lock Screen's rotation programmatically It is very common that during development, one may find very useful to lock/unlock the device screen during specific parts of the code. For instance, while showing a Dialog with information, the developer might want to lock the screen's rotation to prevent the dialog from being dismissed and the current activity from being rebuilt to unlock it again when the dialog is dismissed. Even though we can achieve rotation locking from the manifest by doing : One can do it programmatically as well by doing the following : And then calling the following, to respectively lock and unlock the device rotation and |
| Locking Screen Orientation If you want to lock the screen orientation change of any screen (activity) of your android application you just need to set the Now that activity is forced to always be displayed in "portrait" mode. RemarksReference : https://guides.codepath.com/android/Handling-Configuration-Changes#references (opens new window) |
Read Article
https://develop.horje.com/learn/1434/reference