可以检测屏幕旋转吗?我的意思是 - 只有轮换,这与活动初始化与另一个活动明显不同?
onXxx方法对此似乎没有用,我尝试在flag
中添加/删除starting Intent
(删除似乎没有反映出来,旋转标志就在那里),并尝试为清单中的活动添加 android:configChanges =“orientation”,但onConfigurationChanged
方法似乎每隔一次轮换调用...有线。
我想我错过了一些东西......但是在其他相关主题中没有找到明确的解决方案。
有什么想法吗?
答案 0 :(得分:15)
清单:
<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>
的活动:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
Log.d("tag", "Portrait");
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.d("tag", "Landscape");
else
Log.w("tag", "other: " + orientation);
....
}
尝试此链接
答案 1 :(得分:3)
使用onConfigurationChanged方法来检测屏幕旋转并不是一个好主意。当用户旋转屏幕时,此活动中的配置更改将无法正常工作。
所以我使用这个解决方案来解决这个问题。
public class SampleActivity extends AppCompatActivity {
public static final String KEY_LAST_ORIENTATION = "last_orientation";
private int lastOrientation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (savedInstanceState == null) {
lastOrientation = getResources().getConfiguration().orientation;
}
}
@Override
protected void onStart() {
super.onStart();
checkOrientationChanged();
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
lastOrientation = savedInstanceState.getInt(KEY_LAST_ORIENTATION);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_LAST_ORIENTATION, lastOrientation);
}
private void checkOrientationChanged() {
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation != lastOrientation) {
onScreenOrientationChanged(currentOrientation);
lastOrientation = currentOrientation;
}
}
public void onScreenOrientationChanged(int currentOrientation) {
// Do something here when screen orientation changed
}
}
但代码仍不足以在我的项目中使用它,所以我将此代码应用到我自己的库(https://github.com/akexorcist/ScreenOrientationHelper)。
compile 'com.akexorcist:screenorientationhelper:<latest_version>'
您可以像这样创建基本活动类
public class BaseActivity extends AppCompatActivity implements ScreenOrientationHelper.ScreenOrientationChangeListener {
private ScreenOrientationHelper helper = new ScreenOrientationHelper(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper.onCreate(savedInstanceState);
helper.setScreenOrientationChangeListener(this);
}
@Override
protected void onStart() {
super.onStart();
helper.onStart();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
helper.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
helper.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onScreenOrientationChanged(int orientation) {
}
}
然后使用此基类扩展活动
public class MainActivity extends BaseActivity {
...
@Override
public void onScreenOrientationChanged(int orientation) {
// Do something when screen orientation changed
}
}
完成!
答案 2 :(得分:0)
你为什么不尝试这个?
onCreated 中的获取电话的方向:
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
myOrientation = display.getOrientation();
然后,覆盖方法 onConfigurationChanged 并检查方向是否已更改:
@Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation != myOrientation)
Log.v(tag, "rotated");
super.onConfigurationChanged(newConfig);
}
不要忘记添加到活动中的清单android:configChanges="orientation"
。