gpt4 book ai didi

Android 自定义 View 在方向更改时重新创建两次,第二次没有 onRestoreInstanceState

转载 作者:太空狗 更新时间:2023-10-29 15:08:54 25 4
gpt4 key购买 nike

我创建了一个自定义 View ,它是 RelativeLayout 的子类.该 View 是 Fragment 的一部分设置为保留其实例状态。我想在设备方向改变时保存状态信息(只有一个 bool 值),所以我实现了 onSaveInstanceStateonRestoreInstanceState使用我的自定义扩展名 BaseSavedState . Fragment 都不是,也不是 Activity已经实现了任何处理保存/恢复实例状态的事情。当我更改设备方向时,它会保存状态,使用保存的状态重新创建 View ,然后再次调用 onRestoreInstanceState 重新创建它.

事件的顺序是这样的,showingProgress是我要保存的 bool 值:

View#1 (onSaveInstanceState) showingProgress=true

View#2 (ctor) showingProgress=false
View#2 (onFinishInflate)
View#2 (onRestoreInstanceState) showingProgress=true
View#2 (showProgress)
View#2 (onSaveInstanceState) showingProgress=true

View#3 (ctor) showingProgress=false
View#3 (onFinishInflate)

在那之后, View 被重建,但是因为onRestoreInstanceState永远不会为 View #3 调用,它始终处于初始状态。

为什么它会重新创建 View 两次?如何防止第二个 View 再次重新创建,或将保存的状态分别传递给第三个 View ?

编辑:

Activity 相关部分

private Fragment currentFragment = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.single_fragment);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().show();
showLoginFragment();
}

public void showLoginFragment()
{
final FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
currentFragment = MyFragment.newInstance();
t.replace(R.id.layoutRoot, currentFragment);
t.commit();
}

布局single_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>

MyFragment

public class MyFragment extends RoboFragment
{
// I'm using roboguice
@InjectView(R.id.myButton) private ProgressButton myButton;

public static MyFragment newInstance()
{
Bundle arguments = new Bundle();
// no arguments for now, this comes later
MyFragment fragment = new MyFragment();
fragment.setArguments(arguments);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
setRetainInstance(true);
View result = inflater.inflate(R.layout.my_fragment, container, false);
return result;
}
}

布局my_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.my.package.ProgressButton
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_caption" />

</LinearLayout>

最后是 ProgressButton

的代码
/**
* A Button with an integrated progress spinner. The button can show the progress spinner while some background process
* is running to indicate it can't be clicked again.
*/
public class ProgressButton extends RelativeLayout
{
/** The view holding the button text */
private TextView textView = null;

/** The progress spinner */
private ProgressBar progressSpinner = null;

private boolean showingProgress = false;

public ProgressButton(Context context)
{
super(context);
textView = new TextView(context);
progressSpinner = new ProgressBar(context);
initView();
}

public ProgressButton(Context context, AttributeSet attrs)
{
super(context, attrs);
textView = new TextView(context, attrs);
progressSpinner = new ProgressBar(context, attrs);
initView();
}

public ProgressButton(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
textView = new TextView(context, attrs, defStyle);
progressSpinner = new ProgressBar(context, attrs, defStyle);
initView();
}

/**
* Initializes the view with all its properties.
*/
@SuppressWarnings("deprecation")
private void initView()
{
// remove the background attributes from progressbar and textview, because they should be transparent
if (Build.VERSION.SDK_INT >= 16)
{
textView.setBackground(null);
progressSpinner.setBackground(null);
}
else
{
textView.setBackgroundDrawable(null);
progressSpinner.setBackgroundDrawable(null);
}
}

@Override
protected void onFinishInflate()
{
super.onFinishInflate();
if (!isInEditMode())
{
progressSpinner.setVisibility(View.INVISIBLE);
}
RelativeLayout layout = new RelativeLayout(getContext());
layout.setId(R.id.progressButtonContainer);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.addView(layout, params);

params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_LEFT);
params.leftMargin = 10;
progressSpinner.setClickable(false);
progressSpinner.setId(R.id.progressButtonProgress);
layout.addView(progressSpinner, params);

params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setClickable(false);
textView.setId(R.id.progressButtonText);
layout.addView(textView, params);
}

/**
* Disables the button and shows the progress spinner.
*/
public void showProgress()
{
this.setEnabled(false);
progressSpinner.setVisibility(View.VISIBLE);
showingProgress = true;
}

/**
* Enables the button and hides the progress spinner.
*/
public void hideProgress()
{
this.setEnabled(true);
progressSpinner.setVisibility(View.INVISIBLE);
showingProgress = false;
}

@Override
public Parcelable onSaveInstanceState()
{
Parcelable superState = super.onSaveInstanceState();
return new SavedState(superState, showingProgress);
}

@Override
public void onRestoreInstanceState(Parcelable state)
{
SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
showingProgress = savedState.showProgress;
if(showingProgress)
{
showProgress();
}
else
{
hideProgress();
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void dispatchSaveInstanceState(SparseArray container)
{
super.dispatchFreezeSelfOnly(container);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void dispatchRestoreInstanceState(SparseArray container)
{
super.dispatchThawSelfOnly(container);
}

static class SavedState extends BaseSavedState
{
boolean showProgress = false;

SavedState(Parcelable superState, boolean showProgress)
{
super(superState);
this.showProgress = showProgress;
}

private SavedState(Parcel in)
{
super(in);
this.showProgress = in.readByte() == 0 ? false : true;
}

@Override
public void writeToParcel(Parcel out, int flags)
{
super.writeToParcel(out, flags);
out.writeByte((byte) (showProgress ? 1 : 0));
}

// required field that makes Parcelables from a Parcel
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>()
{
public SavedState createFromParcel(Parcel in)
{
return new SavedState(in);
}

public SavedState[] newArray(int size)
{
return new SavedState[size];
}
};
}
}

最佳答案

在 Daniel Bo 的有益评论之后,我很容易找到解决方案。无论是否存在保存状态, fragment 都会添加到 Activity 中。这会导致在其初始状态添加 View #3。在以下 if 循环中的 Activities onCreate 方法中包装 showLoginFragment(); 可解决问题。

if(savedInstanceState == null)
{
showLoginFragment();
}

关于Android 自定义 View 在方向更改时重新创建两次,第二次没有 onRestoreInstanceState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18873204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com