我在我的应用程序中创建了一个通知。当我有多个通知时,我需要按下每个打开的活动并根据意图传输的参数执行操作。为此我设置了意图标志
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
但是如果您在应用程序打开或关闭时单击通知,那么一切正常。但如果应用程序最小化,则输出错误。
我改变了旗帜
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
现在,当应用程序最小化时,我点击通知就会正常打开。但还有其他问题。打开碎片,然后加载数据。
我是如何生成通知的:
private void generateNotification(Context context, String title, String message,int groupid,Intent data) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
Intent intent = new Intent(context,MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
if (groupid == 1) {
intent.putExtra("guest",data.getStringExtra("guest"));
intent.putExtra("hotel",data.getStringExtra("hotel"));
intent.putExtra("room",data.getStringExtra("room"));
}
if (groupid==4){
intent.putExtra("hotel",data.getStringExtra("hotel"));
intent.putExtra("guest",data.getStringExtra("guest"));
}
if (groupid==5){
intent.putExtra("hotel",data.getStringExtra("hotel"));
}
int requestCode = ("requestCode" + System.currentTimeMillis()).hashCode();
intent.putExtra("group_id",groupid);
intent.setAction(String.valueOf(requestCode));
Log.d("mylogout", "group_id: " + groupid);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
.setTicker("Новое сообщение")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(title)
.setContentText(message)
.setDefaults(Notification.DEFAULT_SOUND)
.build();
notificationManager.notify(requestCode, notification);
}
That is how I get the intent to Activity:
@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("Статус инфо");
}
}
并在MyActivity中的onCreate()中调用此方法。
我希望在按下时收到通知,打开我的Activity并在Activity处于任何状态时设置必要的片段。
如果标志设置为intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
且应用程序最小化错误输出:
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:1123)
at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2042)
at android.app.ActivityThread.performNewIntents(ActivityThread.java:2055)
at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2064)
at android.app.ActivityThread.access$1400(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1194)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4464)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:589)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
检查这个答案 IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager
看起来你的fragmentManager有问题