「Android/端末回転」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→onConfigurationChangedが回転前の縦幅横幅を取得してくる対策) |
|||
| 行44: | 行44: | ||
}); | }); | ||
} | } | ||
| + | 端末によってサイズが取得できない模様。 | ||
| + | |||
| + | 以下のように遅らせてもたまに取得できない模様・・・。 | ||
| + | handler.postDelayed(new Runnable() { | ||
| + | @Override | ||
| + | public void run() { | ||
| + | @Override | ||
| + | public void run() { | ||
| + | Log.i(TAG, "onConfigurationChanged width=" + imageView.getWidth()); | ||
| + | Log.i(TAG, "onConfigurationChanged height=" + imageView.getHeight()); | ||
| + | } | ||
| + | }, 200); | ||
2018年11月15日 (木) 13:28時点における版
回転時のライフサイクル
回転するときにActivityを破棄させない方法
AndroidManifest.xmlに以下を追加
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize"></activity>
すると以下イベントは発生しなくなる。
- onCreate
- onStart
- onRestoreInstanceState
- onResume
- onWindowFocusChanged
- onPause
- onSaveInstanceState
- onStop
- onDestroy
代わりにonConfigurationChangedが発生するので以下コードで捕まえる
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "onConfigurationChanged");
}
参考:https://qiita.com/myoshimu/items/4d891f8e0ec3abaa7662
参考:https://qiita.com/kobakei/items/5f38339dd6528fdc6b5d
onConfigurationChangedが回転前の縦幅横幅を取得してくる対策
thread処理させるとよい
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
Log.i(TAG, "onConfigurationChanged width=" + imageView.getWidth());
Log.i(TAG, "onConfigurationChanged height=" + imageView.getHeight());
}
});
}
端末によってサイズが取得できない模様。
以下のように遅らせてもたまに取得できない模様・・・。
handler.postDelayed(new Runnable() {
@Override
public void run() {
@Override
public void run() {
Log.i(TAG, "onConfigurationChanged width=" + imageView.getWidth());
Log.i(TAG, "onConfigurationChanged height=" + imageView.getHeight());
}
}, 200);
