我需要在onStart()
方法中根据orientation change
的结果以不同的方式进行操作,这取决于onCreate()
方法是如何被调用的。有可能检查到吗?
实际上,我需要在活动启动时做某事,除非用户更改了方向。
我需要清楚地区分当用户仅切换到另一个应用程序并返回时或者用户更改设备方向的情况。
我需要在onStart()
方法中根据orientation change
的结果以不同的方式进行操作,这取决于onCreate()
方法是如何被调用的。有可能检查到吗?
实际上,我需要在活动启动时做某事,除非用户更改了方向。
我需要清楚地区分当用户仅切换到另一个应用程序并返回时或者用户更改设备方向的情况。
根据Nahtan的想法,我写了以下代码:
在onCreate()方法中:
if ( (oldConfigInt & ActivityInfo.CONFIG_ORIENTATION)==ActivityInfo.CONFIG_ORIENTATION ) {
// ... was rotated
} else {
// ... was not rotated
}
在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();
}
}
标志位fromOrientation(告诉状态配置是否已更改)
我们默认情况下认为 onCreate()
不是由于方向改变而调用的。如果方向改变(onRetainNonConfigurationInstance()
),则我们会将标志值设置在首选项中,以便知道 onCreate()
是从方向还是其他方式调用的。
最后,在onDestroy()
中,我们再次重置方向标志。
只需检查捆绑包(bundle):当活动(activity)首次启动时,bundle将为null。如果有方向变化,则应为非空。
从android文档中:
如果在先前关闭后重新初始化活动(activity),则此Bundle包含它最近在onSaveInstanceState(Bundle)中提供的数据。注意:否则它为null。
编辑:我认为您不必在onSaveInstanceState中提供bundle就可以使其在方向更改后为非null,但这很简单。
某些操作
,除非用户更改方向(从描述中复制) - Eugene正如其他人所说,保存状态:
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() {
// ...
}
}
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
}
}
onCreate()
不仅会在屏幕方向改变后发生,还可能在其他情况下发生(例如,在应用程序中转到主屏幕并返回)。 - Eugeneandroid:configChanges="keyboardHidden|orientation"
然后,您只需覆盖onConfigurationChanged方法以获取视图重新排列或其他需要完成的事情。留下您不想执行的内容。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//do your jobs
}
onConfigurationChanged
调用setContentView
,则视图的状态不会被保存和恢复。例如,如果您有一个EditView
,它的内容将被重置。 - Salw如果应用程序是第一次启动,则 mCurrentOrientation
将为 -1。
static int mCurrentOrientation = -1;
public void onStart()
{
int oldOrientation = mCurrentOrientation;
mCurrentOrientation = getCurrentOrientation();
if ((oldOrientation != -1) && (oldOrientation != mCurrentOrientation)) {
// Do your something here.
}
}