通过点击通知,我发布PendingIntent,这是对参数的意图;
在MyActivity中,我得到了这个意图并将其传递给方法onNewIntent(getIntent());
在这个方法中,我取出我的参数,并根据它们来执行操作。
问题是当应用程序关闭或打开时 - 调用方法并且一切正常。但是如果应用程序最小化,则按下通知错误输出。
public class MyActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
onNewIntent(getIntent());
}
...
@Override
public void onNewIntent(Intent intent) {
FragmentManager fragmentManager = getSupportFragmentManager();
Bundle arguments;
Fragment fragment;
String guest;
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.containsKey("group_id")) {
int msg = extras.getInt("group_id");
String hotel = extras.getString("hotel");
switch (msg) {
case 5:
fragment = new PagerLogbookContainer();
arguments = new Bundle();
arguments.putString(ARGUMENT_PAGE_HOTEL, hotel);
fragment.setArguments(arguments);
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
setTitle(hotel);
break;
case 1:
guest = extras.getString("guest");
String room = extras.getString("room");
fragment = new Guest();
arguments = new Bundle();
arguments.putString("guestid", guest);
arguments.putString("hotel", hotel);
arguments.putString("room", room);
fragment.setArguments(arguments);
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.container, fragment, "guest").commit();
break;
case 4:
guest = extras.getString("guest");
fragment = new Survey();
arguments = new Bundle();
arguments.putString("guestid", guest);
arguments.putString("hotel", hotel);
fragment.setArguments(arguments);
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
break;
}
}
} else {
fragment = new StatusInfo();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
setTitle("Статус инфо");
}
}
...
}
}
错误日志:
12-15 04:34:39.995 733-733/com.managment.pavel.managmentgradle E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1365)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1383)
at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:507)
at com.managment.pavel.managmentgradle.MyActivity.onNewIntent(MyActivity.java:158)
at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1119)
at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:1722)
at android.app.ActivityThread.performNewIntents(ActivityThread.java:1734)
at android.app.ActivityThread.handleNewIntent(ActivityThread.java:1742)
at android.app.ActivityThread.access$2300(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:978)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)