我最近偶然发现错误“在onSaveInstanceState之后无法执行此操作”,同时授予(Android M)访问外部存储的权限。 我根据this帖子(我想)解决了这个问题。
现在我遇到了新问题,因为我在尝试更改片段时错误“提交已经调用”。
只要我想切换到某个片段(例如,讨论片段)我就会收到错误“commit already called”,请参阅logcat。
我已经在stackoverflow上搜索了一些解决方案,但我似乎没有看到出现了什么问题,我知道有很多提交但在我的情况下如何处理它? 请有人帮帮我吗?
调用Discussionfragment:
public class DiscussionsActivity extends BaseActivity
{
..
discussionsFragment = DiscussionsFragment.getInstance(id);
changeContent(discussionsFragment, R.id.content, false); <---
..
}
BaseActivity:
public class BaseActivity extends AppCompatActivity {
protected Fragment currentFragment;
protected MainFragment mainFragment;
private FragmentManager fragmentManager;
private FragmentTransaction transaction;
private boolean mReturningWithResult = false;
public void changeContent(Fragment fragment, int content, boolean inBackStack) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
try {
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
fragmentManager = getSupportFragmentManager();
String newTag = ((Object) fragment).getClass().getName() + ":" + fragmentManager.getBackStackEntryCount();
transaction = fragmentManager.beginTransaction().replace(content, fragment, newTag);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
if (inBackStack) {
transaction.addToBackStack(fragment.getClass().getSimpleName());
} else
mReturningWithResult = true; <--- handle onSaveInstanceState error in onResume
try {
fragmentManager.popBackStackImmediate();
transaction.commit();
} catch (IllegalStateException ignored) {
// There's no way to avoid getting this if saveInstanceState has already been called.
}
currentFragment = fragment;
}
@Override
public void onResume() {
super.onResume();
if (mReturningWithResult) {
fragmentManager.popBackStackImmediate();
if (currentFragment != mainFragment) {
transaction.commit(); <---- HERE THE CRASH HAPPENS
}
}
// Reset the boolean flag back to false for next time.
mReturningWithResult = false;
}
logcat的:
FATAL EXCEPTION: main
Process: some.com.app, PID: 17374
java.lang.RuntimeException: Unable to resume activity {some.com.app/some.com.app.UI.Discussion.DiscussionsActivity_}: java.lang.IllegalStateException: commit already called
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3103)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: commit already called
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662)
at some.com.app.UI.Base.BaseActivity.onResume(BaseActivity.java:144) <--
答案 0 :(得分:1)
删除全局FragmentTransaction并在本地声明
public void changeContent(Fragment fragment, int content, boolean inBackStack) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
try {
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
}
fragmentManager = getSupportFragmentManager();
String newTag = ((Object) fragment).getClass().getName() + ":" + fragmentManager.getBackStackEntryCount();
FragmentTransaction transaction = fragmentManager.beginTransaction().replace(content, fragment, newTag);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
if (inBackStack) {
transaction.addToBackStack(fragment.getClass().getSimpleName());
} else
mReturningWithResult = true; <--- handle onSaveInstanceState error in onResume
try {
transaction.commit();
} catch (IllegalStateException ignored) {
// There's no way to avoid getting this if saveInstanceState has already been called.
}
currentFragment = fragment;
}
答案 1 :(得分:1)
您可以在commit语句后尝试使用相同的片段事务提交两次。如果先前的事务已经提交,则创建一个新的片段事务。
还建议您确保commit()
使用调试点。
public void changeContent(Fragment fragment, int content, boolean inBackStack) {
...........
...........
try {
fragmentManager.popBackStackImmediate();
transaction.commit();
} catch (IllegalStateException ignored) {
// There's no way to avoid getting this if saveInstanceState has already been called.
...........
...........
}
@Override
public void onResume() {
super.onResume();
...........
...........
//This method would be called at second time.
transaction.commit(); <---- HERE THE CRASH HAPPENS
// Reset the boolean flag back to false for next time.
mReturningWithResult = false;
...........
...........
}