I know this is a duplicate question, and I know that when the device is rotated, the OnCreate method is called again, and I want to keep it like this; I don't want to go through the steps of overriding onConfigurationChanged or sub-classing my Application class because android is doing the reloading in the best way (especially on saving current values and status).
However, I have some commands inside the OnCreate method which I don't want to execute them if the Activity is reloaded due to Rotation, so is there any property/method that can tell if OnCreate is triggered due to first Application start or due to Orientation change?
For example:
if(< OnCreate triggered due to Orientation > == false)
// Execute my commands...
else
// Do nothing...
Alternatively, do we have any eve开发者_如何学Gonts in Android to be triggered before OnCreate or before OnConfigurationChanged?
Why not store the current orientation in SharedPreferences and compare the current orientation to the stored value in onCreate (and then save the new orientation to SharedPreferences)?
That should provide the functionality that you require, unless I've misunderstood something.
Try using onSaveInstanceState, if you want to save some state information before your activity gets destroyed and then onRestoreInstanceState to retrieve it.
You can also use getRequestedOrientation() in onCreate to get the new Orientation.
You can also use this in onCreate()
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
The Activity class has a special method called onRetainNonConfigurationInstance(). This method can be used to pass an arbitrary object your future self and Android is smart enough to call this method only when needed.
Read more here.
精彩评论