Preferenceで設定したフラグを用いてradioButtonの有効/無効を決定するプログラムを書いていたのですが、
最初は期待通りの動作をするもの、一度画面を回転させてからこれらの機能が聞かなくなってしまいます。
具体的には以下の画像のような感じです。左が回転前、右が回転後となります。
[album]929[/album] [album]930[/album]
また、記述したプログラムを以下に示します。このプログラムは別のActivityで実装した機能を再利用するため、
当該Activityを継承して利用するようなスタイルになっています。
package spark.sin.virtualtripv2;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Point;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.internal.view.menu.MenuBuilder;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class SelectMapActivity extends MapPreferencePointActivity {
// すでに長押しで位置情報が取れるマップビュー(Map要素すら親が持っている)
private boolean mIsFirstCreate = true;
final int destWidth = 0;
final int destHeight = 1;
final int preferenceCode = 1024;
private int getID(int ID){
return getResources().getInteger(ID);
}
private String getStringFromID(int ID){ return getResources().getString(ID); }
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); // マップ生成の他にもいろいろな機能を実装する。
if(mIsFirstCreate) {
if(savedInstanceState == null) startPreference();
}
/* その他フラグメントのコミット */{
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//transaction.add(getID(R.integer.conditionFragmentID), conditionFragment);
transaction.add(R.id.MapPreferenceLayout, new SetMiniPreferenceFragment());
//transaction.add(getID(R.integer.posListUpFragmentID), posListUpFragment);
transaction.commit();
}
// マーカークリック時の動作を上書き(できる?) -> 見た感じ無名クラスらしい
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// Fragmentの更新・アニメーション
return true;
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
/* Fragmentの初期位置設定 */{
Point pt = getViewSize((FrameLayout) findViewById(R.id.SelectMapBaseFrame));
// 縦横判定
FrameLayout view = (FrameLayout)findViewById(R.id.MapPreferenceLayout);
if(pt.y >= pt.x){
// 縦(相対指定)
view.setTranslationY(-pt.y / 2.0f);
} else {
// 横
view.setTranslationX(-pt.x / 2.0f);
}
}
// マップを移動させたら設定画面は隠す
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
Point fieldSize = getViewSize(findViewById(R.id.SelectMapBaseFrame));
Point contentSize = getViewSize(findViewById(R.id.MapPreferenceLayout));
if(!findViewById(R.id.buttonCondSubmit).isEnabled()) return;
Animation animation = setFragmentAnimation(destHeight,-contentSize.y/2.0f, false, destWidth,-contentSize.x/2.0f, false);
final View view = findViewById(R.id.MapPreferenceLayout);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
AllViewEnableSetting.SetAllFlags((ViewGroup)view, false);
View mapView = findViewById(R.id.MapViewLayout);
mapView.bringToFront();
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
view.startAnimation(animation);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_select_map, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.MapSelect_GetDetailSetting){
if(findViewById(R.id.buttonCondSubmit).isEnabled()) return true;
Point fieldSize = getViewSize(findViewById(R.id.SelectMapBaseFrame));
Point contentSize = getViewSize(findViewById(R.id.MapPreferenceLayout));
Animation animation = setFragmentAnimation(destHeight,contentSize.y/2.0f,true , destWidth, contentSize.x/2.0f, true);
final View view = findViewById(R.id.MapPreferenceLayout);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
view.bringToFront();
AllViewEnableSetting.SetAllFlags((ViewGroup)view, true);
CheckMiniPreferenceButtonEnable(false);
}
@Override
public void onAnimationEnd(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
});
view.startAnimation(animation);
}if(id == R.id.setting){
startPreference();
}
return super.onOptionsItemSelected(item);
}
private Point getViewSize(View view){
Point ans = new Point();
ans.x = view.getWidth();
ans.y = view.getHeight();
return ans;
}
private Point getViewPos(View view){
int[] Pos = {-1, -1};
view.getLocationInWindow(Pos);
Point ans = new Point();
ans.x = Pos[0]; ans.y = Pos[1];
return ans;
}
private Animation setFragmentAnimation(int hDest, float hMovement, boolean isHReverse,int wDest, float wMovement, boolean isWReverse){
// リスナは後で各自定義する
Point fieldSize = getViewSize(findViewById(R.id.SelectMapBaseFrame));
Point contentSize = getViewSize(findViewById(R.id.MapPreferenceLayout));
final boolean isWide = fieldSize.x > fieldSize.y;
TranslateAnimationBuilder builder = new TranslateAnimationBuilder();
float[] trans = {0,0,0,0};
final int dest = isWide ? wDest : hDest;
final float movement = isWide ? wMovement : hMovement;
switch(dest) {
case destWidth:
if(isHReverse) {
trans[0] -= movement;
} else {
trans[1] += movement;
}
break;
case destHeight:
if(isHReverse) {
trans[2] -= movement;
} else {
trans[3] += movement;
}
break;
}
int[] mode = {Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF,
Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF};
builder.setPosition(trans);
builder.setType(mode);
builder.setOption(500, new DecelerateInterpolator(), false);
return builder.export();
}
public void CheckMiniPreferenceButtonEnable(boolean isFirst){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if(prefs.getBoolean( getStringFromID(R.string.selPre_isConstTime), false) ){
final Integer Val = new Integer(
prefs.getString( getStringFromID(R.string.selPre_getConstTime), null ));
ViewGroup group = (ViewGroup)findViewById(R.id.timeRadioGroup);
AllViewEnableSetting.SetAllFlags(group, false);
if(Val != null) {
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.timeRadioGroup);
radioGroup.check(radioGroup.getChildAt(Val).getId());
}
}
if(prefs.getBoolean( getStringFromID(R.string.selPre_isConstWeather), false) ){
final Integer Val = new Integer(
prefs.getString( getStringFromID(R.string.selPre_getConstWeather), null ));
ViewGroup group = (ViewGroup)findViewById(R.id.weatherRadioGroup);
AllViewEnableSetting.SetAllFlags(group, false);
if(Val != null) {
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.weatherRadioGroup);
radioGroup.check(radioGroup.getChildAt(Val).getId());
}
}
if(prefs.getBoolean( getStringFromID(R.string.selPre_isConstGenre), false) ){
final String Val = prefs.getString(getStringFromID(R.string.selPre_getConstGenre), "");
EditText view1 = (EditText)findViewById(R.id.GenreInputView);
view1.setText(Val);
view1.setEnabled(false);
}
Button button = (Button)findViewById(R.id.gotoPrevPlace);
button.setEnabled(isFirst);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == preferenceCode) {
if (!findViewById(R.id.buttonCondSubmit).isEnabled()) {
Point contentSize = getViewSize(findViewById(R.id.MapPreferenceLayout));
Animation animation = setFragmentAnimation(destHeight, contentSize.y / 2.0f, true, destWidth, contentSize.x / 2.0f, true);
final View view = findViewById(R.id.MapPreferenceLayout);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
view.bringToFront();
AllViewEnableSetting.SetAllFlags((ViewGroup) view, true);
CheckMiniPreferenceButtonEnable(false);
}
@Override
public void onAnimationEnd(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
});
view.startAnimation(animation);
}
CheckMiniPreferenceButtonEnable(false);
super.onActivityResult(requestCode, resultCode, intent);
}
}
private void startPreference(){
Intent intent = new Intent(getApplicationContext(), SetMapPreferenceActivity.class);
startActivityForResult(intent, preferenceCode);
mIsFirstCreate = false;
}
}
xmlはビューに表れているボタンなどのリソース定義(上から縦・横)です。これらはFragmentとしてビューに貼りつけています。
package spark.sin.virtualtripv2;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by *** on 2015/03/14.
*/
public class AllViewEnableSetting {
public static void SetAllFlags(ViewGroup viewGroup, boolean isEnable){
for(int i=0; i<viewGroup.getChildCount(); ++i){
// superクラスではinstanceofはtrueとなる。
if(viewGroup.getChildAt(i) instanceof ViewGroup) {
Log.d("Enable", "Get ViewGroup");
SetAllFlags((ViewGroup) viewGroup.getChildAt(i), isEnable);
}
View view = viewGroup.getChildAt(i);
view.setEnabled(isEnable);
Log.d("Enable", "View Change");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Blank" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffefefef">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="現在地:"
android:id="@+id/textNowPosition"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="時刻"
android:id="@+id/textView3"
android:layout_gravity="center_vertical" />
<RadioGroup
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:id="@+id/timeRadioGroup"
android:layout_gravity="center_vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朝"
android:id="@+id/timeMorning"
android:layout_weight="1"
android:enabled="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="昼"
android:id="@+id/timeAfternoon"
android:layout_weight="1"
android:enabled="true"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="夕方"
android:id="@+id/timeEvening"
android:layout_weight="1"
android:enabled="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="夜"
android:id="@+id/timeNight"
android:layout_weight="1"
android:enabled="true"
android:checked="false" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天候"
android:id="@+id/textView"
android:layout_gravity="center_vertical" />
<RadioGroup android:layout_height="wrap_content"
android:layout_width="wrap_content" android:orientation="horizontal"
android:layout_weight="1"
android:id="@+id/weatherRadioGroup"
android:layout_gravity="center_vertical">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="晴れ"
android:id="@+id/weatherSunny"
android:layout_weight="1"
android:enabled="true"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="曇り"
android:id="@+id/wheatherCloudy"
android:layout_weight="1"
android:enabled="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雨"
android:id="@+id/weatherRainy"
android:layout_weight="1"
android:enabled="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雪"
android:id="@+id/weatherSnowy"
android:layout_weight="1"
android:enabled="true" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ジャンル"
android:id="@+id/textView2"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/GenreInputView" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="やっぱり前に戻る"
android:id="@+id/gotoPrevPlace"
android:layout_weight="1"
android:enabled="false" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="検索!!"
android:id="@+id/buttonCondSubmit"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"></LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Blank" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:background="#ffefefef">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="現在地:"
android:id="@+id/textNowPosition"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="8dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="時刻"
android:id="@+id/textView3"
android:layout_gravity="center_vertical" />
<RadioGroup
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:id="@+id/timeRadioGroup" >
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="朝"
android:id="@+id/timeMorning"
android:layout_weight="1"
android:enabled="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="昼"
android:id="@+id/timeAfternoon"
android:layout_weight="1"
android:enabled="true"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="夕方"
android:id="@+id/timeEvening"
android:layout_weight="1"
android:enabled="true"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="夜"
android:id="@+id/timeNight"
android:layout_weight="1"
android:enabled="true"
android:checked="false" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天候"
android:id="@+id/textView"
android:layout_gravity="center_vertical" />
<RadioGroup android:layout_height="wrap_content"
android:layout_width="wrap_content" android:orientation="horizontal"
android:layout_weight="1"
android:id="@+id/weatherRadioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="晴れ"
android:id="@+id/weatherSunny"
android:layout_weight="1"
android:enabled="true"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="曇り"
android:id="@+id/wheatherCloudy"
android:layout_weight="1"
android:enabled="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雨"
android:id="@+id/weatherRainy"
android:layout_weight="1"
android:enabled="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="雪"
android:id="@+id/weatherSnowy"
android:layout_weight="1"
android:enabled="true" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ジャンル"
android:id="@+id/textView2"
android:layout_gravity="center_vertical" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/GenreInputView" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="やっぱり前に戻る"
android:id="@+id/gotoPrevPlace"
android:layout_weight="1"
android:enabled="false" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="検索!!"
android:id="@+id/buttonCondSubmit"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
状況なのですが、個人的に少し調査をしたうえで原因が分からない状態にあります。
該当する処理はCheckMiniPreferenceButtonEnableメソッドで行っています。どちらの場合もこのメソッドを適切な
タイミングで呼び出しており、またPreferenceの値も保持されていることをデバッグにより確認しました。また、
どちらの向きの場合も画面を回転させるまでは正常に動くことを確認しています。
しかし症状発生時の表示結果はラジオスイッチが無効にならないばかりか、現状必ず無効になるはずの「やっぱり前に戻る」
ボタンさえ無効化されない状態にありました。なお、縦横共に対応するViewのidは同じであるようにしており、さらに
アニメーションにおいて同じ状況下(Layoutを動かした)でこの操作が有効であることは確認しています。
すいませんが知識のある方、もし原因が分かりましたら知恵を貸していただけると幸いです。よろしくお願いします。