I need to act differently in onStart()
method depending on how onCreate()
was called in result
of orientation change
or not. Is that
Following Nahtan's idea I wrote this:
in onCreate():
if ( (oldConfigInt & ActivityInfo.CONFIG_ORIENTATION)==ActivityInfo.CONFIG_ORIENTATION ) {
// ... was rotated
} else {
// ... was not rotated
}
in onDestroy():
super.onDestroy();
oldConfigInt = getChangingConfigurations();
public class MyActivity extends Activity {
boolean fromOrientation=false;
SharedPreferences myPrefLogin;
Editor prefsEditor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefsEditor = myPrefLogin.edit();
myPrefLogin = this.getSharedPreferences("myPrefs", Context.MODE_WORLD_READABLE);
fromOrientation = myPrefLogin.getString("fromOrient", false);
if(fromOrientation) {
// do as per need--- logic of orientation changed.
} else {
// do as per need--- logic of default onCreate().
}
}
@Override
public Object onRetainNonConfigurationInstance() {
prefsEditor.putString("fromOrient", true);
prefsEditor.commit();
return null;
}
@Override
protected void onDestroy() {
if(fromOrientation) {
prefsEditor.putString("fromOrient", false);
prefsEditor.commit();
}
super.onDestroy();
}
}
flag fromOrientation(tells the state either configuration had been change or not)
above we had done like by defalut we are considering that onCreate()
is not called from by changing orientation. If orientation changed(onRetainNonConfigurationInstance()
) then we set the flag value in preference which let us know whether onCreate()
is called from on orientation or not.
And finally onDestroy()
we again reset the orientation flag.
Try getChangingConfigurations. Use this to discover when the activity is being destroyed because of an orientation change, set a flag, and check that flag when onCreate is called.
This has the advantage of letting you know why the activity is restarting without overriding orientation configuration in the manifest.
As others have said, save the state:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null) {
// app started afresh, don't check orientation
/* do application first-run stuff */
} else {
// re-started, so check for orientation change
boolean orientationChanged = false;
if(savedInstanceState != null) {
int lastOrientation = savedInstanceState.getInt("last_orientation");
orientationChanged = (getOrientation() != lastOrientation);
}
if(orientationChanged) {
// orientation changed
/* do stuff */
} else {
// orientation has not changed
/* do stuff */
}
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("last_orientation", getOrientation());
}
private int getOrientation() {
// ...
}
}
You can try the following. On each onStart, you can save the current orientation (take a look here). If it is the first onStart call, do nothing, on other calls, check if the orientation has changed and do what you need.
private int m_currentOrientation = -1;
public void onStart()
{
int oldOrientation = m_currentOrientation; //
m_currentOrientation = getCurrentOrientation(); // As in the link above
if ((oldOrientation != -1) && (oldOrientation != m_currentOrientation))
{
// Do what you need
}
}
The mCurrentOrientation
will be -1 if the app is starting for the first time.
static int mCurrentOrientation = -1;
public void onStart()
{
int oldOrientation = mCurrentOrientation;
mCurrentOrientation = getCurrentOrientation();
if ((oldOrientation != -1) && (oldOrientation != mCurrentOrientation)) {
// Do your something here.
}
}
If I understood your question correctly, I think this should work for you.