「Android/端末回転」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→何度かループさせる) |
(→何度かループさせる) |
||
行63: | 行63: | ||
private final Handler handler = new Handler(); | private final Handler handler = new Handler(); | ||
private final Runnable runnable = new Runnable() { | private final Runnable runnable = new Runnable() { | ||
− | + | @Override | |
− | + | public void run() { | |
− | + | ||
Log.i(TAG, "onConfigurationChanged width=" + imageView.getWidth()); | Log.i(TAG, "onConfigurationChanged width=" + imageView.getWidth()); | ||
Log.i(TAG, "onConfigurationChanged height=" + imageView.getHeight()); | Log.i(TAG, "onConfigurationChanged height=" + imageView.getHeight()); | ||
行78: | 行77: | ||
handler.postDelayed(runnable, 250); | handler.postDelayed(runnable, 250); | ||
} | } | ||
− | + | } | |
− | } | + | }; |
@Override | @Override | ||
public void onConfigurationChanged(Configuration newConfig) { | public void onConfigurationChanged(Configuration newConfig) { |
2019年9月17日 (火) 16:04時点における最新版
目次
回転時のライフサイクル
回転するときに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);
何度かループさせる
import android.os.Handler; private int checkCount = 0; private int prevHeight = 0; private final Handler handler = new Handler(); private final Runnable runnable = new Runnable() { @Override public void run() { Log.i(TAG, "onConfigurationChanged width=" + imageView.getWidth()); Log.i(TAG, "onConfigurationChanged height=" + imageView.getHeight()); int height = imageView.getHeight(); if (height != prevHeight) { //LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height); //imageView.setLayoutParams(params); prevHeight = height; } if (checkCount <= 10) { checkCount = checkCount + 1; handler.postDelayed(runnable, 250); } } }; @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); checkCount = 0; handler.post(runnable); }