不会从通知栏调用片段

时间:2014-05-19 10:17:56

标签: android android-fragments notifications

我从通知栏点击事件调用片段类。但是,当我点击该通知栏时,我无法打开我已经调用过的片段类,我已经用一个静态布尔变量检查了它。仍然没有用。

通知代码:

private void generateNotification(Context context, String message) {

        int icon = R.drawable.kutch_smallpng;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        // Notification notification = new Notification(icon, message, when);
        NotificationCompat.Builder notificationBuilder = null;
        String title = context.getString(R.string.app_name);
        int requestID = (int) System.currentTimeMillis();

        Intent notificationIntent = new Intent(context, MainMenuActivity.class);
        // set intent so it does not start a new activity

        if (notificationIntent != null) {
            notificationIntent.setAction("CurrentOffersFragment" + requestID);

        }

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, requestID,
                notificationIntent, 0);
        // notification.setLatestEventInfo(context, title, message, intent);
        // notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationBuilder = new NotificationCompat.Builder(
                getApplicationContext()).setWhen(when).setContentText(message)
                .setContentTitle(title).setSmallIcon(icon).setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_LIGHTS)
                .setContentIntent(intent);
        Uri defaultRingtoneUri = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notificationBuilder.setSound(defaultRingtoneUri);
        notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);

        Notification notification = notificationBuilder.build();
        notificationManager.notify((int) when, notification);

        Log.v("My Message is", message);
        notificationClicked = true;

    }

现在在这样的MainMenuActivity's onCreate()方法中调用。

  Intent intent = getIntent();

        if (intent != null) {

            Log.e("Is Clicked", "true");

            try {
                if (notificationClicked == true
                        && intent.getAction().equals("CurrentOffersFragment")) {
                    Fragment fragment = new CurrentOffersFragment();

                    // txtMainTitlebar.setText("Current Offers");
                    fragmentManager = getSupportFragmentManager();

                    FragmentTransaction ft = fragmentManager.beginTransaction();
                    ft.add(R.id.container, fragment);
                    fragmentStack.push(fragment);
                    ft.commit();

                }
            } catch (Exception e) {
                // TODO: handle exception

            }

            // }

        }

但在这里它没有满足notificationClicked == true && intent.getAction().equals("CurrentOffersFragment")的条件。

所以请为此提供帮助。这是合适的。

1 个答案:

答案 0 :(得分:1)

最后我得到了回答。我改变了什么

在MainMenuActivity中创建静态布尔变量

public static boolean notificationClicked = false;

然后在通知类

中使其成立
notificationClicked = true;

并使用

检入MainMenuActivity
 Intent intent = getIntent();

        if (intent != null) {

            try {
                if (notificationClicked == true) {
                    Fragment fragment = new CurrentOffersFragment();

                    txtMainTitlebar.setText("Current Offers");
                    fragmentManager = getSupportFragmentManager();

                    FragmentTransaction ft = fragmentManager.beginTransaction();
                    ft.add(R.id.container, fragment);
                    fragmentStack.push(fragment);
                    ft.commit();

                }
            } catch (Exception e) {
                // TODO: handle exception

            }

            // }

             }

问题出在这里

 if (notificationIntent != null) {
        notificationIntent.setAction("CurrentOffersFragment" + requestID);

  }

所以我刚刚删除了那个。像魅力一样工作。