Sử dụng onConfigurationChế độ thay đổi để phát hiện xoay màn hình không phải là ý tưởng hay. Thay đổi cấu hình trong hoạt động này sẽ không hoạt động chính xác khi người dùng xoay màn hình.
Vì vậy, tôi sử dụng giải pháp này để giải quyết vấn đề này.
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
}
}
Nhưng mã vẫn chưa đủ để sử dụng trong dự án của tôi, vì vậy tôi đã áp dụng mã này vào thư viện của riêng mình (https://github.com/akexorcist/ScreenOrientationHelper).
compile 'com.akexorcist:screenorientationhelper:<latest_version>'
Bạn có thể tạo lớp hoạt động cơ sở như thế này
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) {
}
}
Sau đó, mở rộng hoạt động với lớp cơ sở này
public class MainActivity extends BaseActivity {
...
@Override
public void onScreenOrientationChanged(int orientation) {
// Do something when screen orientation changed
}
}
Xong!
bạn có nghĩa là nếu nó chuyển từ chế độ ngang sang dọc hoặc ngược lại? – Pengume
không phải là chủ đề này giống nhau không? : http://stackoverflow.com/questions/4843809/how-do-i-detect-screen-rotation – Viren
Trong phương thức onConfigurationChanged, bạn có thể phát hiện loại ConfigurationChanged nào xảy ra, như xoay màn hình. – mikepenz