单击RecyclerView项上的onSaveInstanceState之后,无法执行此操作 [英] Can not perform this action after onSaveInstanceState on RecyclerView item click
问题描述
我有
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
转动我的设备(更改配置)并单击RecyclerView中的项目后,
.单击RecyclerView项必须启动一个新片段.
这是MainActivity中的项目单击方法:
after turning my device (changing configuration) and clicking on item in RecyclerView. Clicking on RecyclerView item have to start a new fragment.
Here is on item click method in MainActivity:
public class MainActivity extends AppCompatActivity implements
MyFragment.OnFragmentInteractionListener,
ListAdapter.OnRecyclerViewItemClickListener{
// ... some code ...
@Override
public void OnItemClickListener(int position) {
MyFragment myFragment = MyFragment.newInstance(position);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.activity_main_layout, myFragment,
myFragment.TAG);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
我有错误
fragmentTransaction.commit();
这是适配器中的物品点击代码:
Here is item clicking code in adapter:
public class ListAdapter extends
RecyclerView.Adapter<ListAdapter.ViewHolder> implements Serializable {
private OnRecyclerViewItemClickListener mListener;
/*... some code here ... */
// constructor
ListAdapter(Context context, ArrayList<ListItem> notesArrayList) {
mListener = (OnRecyclerViewItemClickListener) context;
}
public interface OnRecyclerViewItemClickListener {
void OnItemClickListener(int position);
}
class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
ConstraintLayout layout;
TextView title;
ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.note_title);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
mListener.OnItemClickListener(position);
}
}
}
我还使用MainActivity中的onSaveInstantState和onRestoreInstanceState来保存RecyclerView数据.谢谢.
P.S.我在这里找到了解决方案:
And I also use onSaveInstantState and onRestoreInstanceState in MainActivity to keep RecyclerView data. Thanks.
P.S.
I found a solution here:
commitAllowingStateLoss()
但是它对我不起作用,单击它后什么也没有发生...
P.P.S.完整的堆栈跟踪:
but it doesn't work for me, with it, nothing happens after clicking...
P.P.S.
Full stacktrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dojo.stackoverflowproblem, PID: 17220
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2044)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2067)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:680)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:634)
at com.dojo.stackoverflowproblem.notepad.MainActivity.OnItemClickListener(MainActivity.java:118)
at com.dojo.stackoverflowproblem.notepad.ListAdapter$ViewHolder.onClick(ListAdapter.java:98)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
推荐答案
我遇到了类似的问题.深入研究.commit()或.show()的代码,您可以通过FragmentManager.java的checkStateLoss()找到它.还有一个函数isStateSaved().因此,您可以看到这是解决问题的简单方法:
I met a similar issue. Trace deep into the codes of .commit() or .show(), and you can find it go through checkStateLoss() of FragmentManager.java. There also be a function isStateSaved(). So, you can see this is a simple way to fix the problem:
if(!getSupportFragmentManager().isStateSaved()) { //this to avoid IllegalStateException
//This makes it safe to do your fragment.commit(); or fragmentDialog.show() etc. here.
}
在FragmentManager.java中:
in FragmentManager.java:
checkStateLoss()检查mStateSaved引发异常.因此,!isStateSaved()只是不引发异常的条件.
checkStateLoss() checks mStateSaved to throw the exception. So, !isStateSaved() is just the condition not to throw the exception.
private void checkStateLoss() {
if (mStateSaved) {
throw new IllegalStateException(
"Can not perform this action after onSaveInstanceState");
}
if (mNoTransactionsBecause != null) {
throw new IllegalStateException(
"Can not perform this action inside of " + mNoTransactionsBecause);
}
}
@Override
public boolean isStateSaved() {
return mStateSaved;
}
这篇关于单击RecyclerView项上的onSaveInstanceState之后,无法执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!