从意图处理程序提交片段事务时出错

时间:2015-06-15 08:21:04

标签: android

我正在尝试编写简单的音乐播放器并遇到这样的问题。在我的UI中,我有几个片段,用于显示歌曲的不同模式(例如,所有音乐,所选文件夹中的音乐,播放列表中的音乐等)。我也有一个播放音乐的服务。

当用户关闭播放器处于播放状态时,我想保存UI状态(即当前显示片段)并在再次打开播放器后恢复此状态(如果用户关闭播放器处于暂停状态,则它不需要)。

对于保存状态,我使用上面提到的Service。在我的活动的onDestroy()方法中,我将所有需要的数据用于恢复意图并调用startService(intent)。服务将此数据保存到本地变量。在我的活动的onCreate()方法中,我有意地调用startService,包含已保存状态的请求。在服务中,我将保存的数据置于意图中,并使用Activity方法将其发送到LocalBroadcastManager.getInstance(this).sendBroadcast(intent)。在我的BroadcastListener的内部Activity类中,我收到此意图并提取数据。看来没关系。

但是当我尝试恢复用户关闭播放器时显示的Fragments时问题就开始了。应用程序崩溃unhandled exception.这是后台堆栈:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
        at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1377)
        at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1395)
        at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
        at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
        at com.borune.test.MainActivity.switchFragments(MainActivity.java:58)
        at com.borune.test.MainActivity$MyServiceListener.onReceive(MainActivity.java:102)
        at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
        at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
        at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5032)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

我已经在Google上搜索了这个例外情况,但所有提议的解决方案对我的情况都没有帮助。我写了一个重复描述问题的简单项目。这是代码:

MainActivity.java

public class MainActivity extends ActionBarActivity {

private int current_fragment;
private FragmentA fragmentA = null;
private FragmentB fragmentB = null;
private MyServiceListener myServiceListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    current_fragment = 1;
    switchFragments(null);
    myServiceListener = new MyServiceListener();

    LocalBroadcastManager.getInstance(this).registerReceiver(myServiceListener,new IntentFilter(MyService.RESPONSE_RESTORE));

    requestState();
}

public void switchFragments(View view) {
    switch(current_fragment) {
        case 1 : {
            if(fragmentA == null) {
                fragmentA = FragmentA.newInstance();
            }

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container, fragmentA);
            current_fragment = 1;
            transaction.commit();

            current_fragment = 2;

            break;
        }
        case 2 : {
            if(fragmentB == null) {
                fragmentB = FragmentB.newInstance();
            }

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container, fragmentB);
            current_fragment = 1;
            transaction.commit();

            current_fragment = 1;

            break;
        }

    }
}

protected void onDestroy(){
    super.onDestroy();

    Log.d("log", "onDestroy() called, saving fragment number");
    Intent intent = new Intent(this,MyService.class);
    intent.setAction(MyService.SAVE_STATE);
    intent.putExtra(MyService.FRAGMENT_NUMBER,current_fragment);
    startService(intent);
}


private class MyServiceListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if(action.equals(MyService.RESPONSE_RESTORE)) {
            current_fragment = intent.getIntExtra(MyService.FRAGMENT_NUMBER,-1);
            Log.d("log", "received fragment number, restoring now");
            switchFragments(null);
        }
    }
}

private void requestState() {
    Intent intent = new Intent(this,MyService.class);
    intent.setAction(MyService.REQUEST_RESTORE);
    startService(intent);
 }
 }

MyService.java

public class MyService extends Service {

public static final String REQUEST_RESTORE = "action.REQUEST_RESTORE";
public static final String RESPONSE_RESTORE = "action.RESPONSE_RESTORE";
public static final String SAVE_STATE = "action.SAVE_STATE";
public static final String FRAGMENT_NUMBER = "FRAGMENT_NUMBER";

private int fragment_number = -1;

public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    String action = intent.getAction();
    if(action.equals(REQUEST_RESTORE)) {
        if(fragment_number != -1)
            sendState();
    } else if (action.equals(SAVE_STATE)) {
        fragment_number = intent.getIntExtra(FRAGMENT_NUMBER,-1);
    }
    return START_NOT_STICKY;
}

private void sendState() {
    Intent intent = new Intent(RESPONSE_RESTORE);
    intent.putExtra(FRAGMENT_NUMBER,fragment_number);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
 }
 }

在这个应用程序中,我有一个带有容器的布局和带有Button的{​​{1}}。关闭应用程序并再次打开它后,我收到相同的例外。

有人可以解释一下,为什么会出现这个错误以及如何解决它?

1 个答案:

答案 0 :(得分:5)

该主题将解释会发生什么:getting exception "IllegalStateException: Can not perform this action after onSaveInstanceState"

简短回答但可能有危险:使用commitAllowingStateLoss()代替commit(),但如果您不小心,可能会破坏某些内容。

至于我做了什么,我创建了一个名为“isSafeToCommitFragment”的布尔标志,默认情况下将其设置为false,然后将值分配给Activity生命周期,如下所示:

  • onDestroy():false
  • onPause():false
  • onResume():false
  • onStart():false
  • onStop():false
  • onPostResume():true

在致电fragmentTransaction.commit()之前,请按以下方式检查标志:

if (isSafeToCommitFragment) {
    fragmentTransaction.commit();
}