无法从Notification PendingIntent创建Android堆栈 [英] Android back-stack not being created from Notification PendingIntent

查看:71
本文介绍了无法从Notification PendingIntent创建Android堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在收到通知时,如果应用程序不在内存中,我会遇到问题.不会创建后栈.我已经按照开发人员指南进行了操作.请向我展示我错过的一点,否则我将不得不通过我的 HomeActivity 路由所有意图,以便根据以下意图手动创建后退堆栈.

I'm having issue if the app isn't in memory when the notification is followed. The backstack won't be created. I've followed the steps according to the developers guide. Please show me the bit I've missed otherwise I'll have to route all intents through my HomeActivity in order to create the backstack manually on following intent.

AndroidManifest.xml :

<activity
    android:name=".activity.HomeActivity"
    android:clearTaskOnLaunch="true"
    android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
    android:icon="@drawable/actionbar_logo"
    android:label="@string/activity_label_home"
    android:launchMode="singleTask"
    android:parentActivityName=".activity.Start"
    android:windowSoftInputMode="stateAlwaysHidden" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".activity.Start" />
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
<activity
    android:name=".activity.ChatActivity"
    android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
    android:label="@string/activity_label_in_chat"
    android:parentActivityName=".activity.HomeActivity"
    android:windowSoftInputMode="stateHidden"
    tools:ignore="UnusedAttribute" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".activity.HomeActivity" />

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="vnd.android.cursor.item/vnd.myapp.chat" />
    </intent-filter>
</activity>

建筑物通知:

final String chatId = cursor.getString(cursor.getColumnIndexOrThrow(MessageColumns.CHAT));
final Intent chat = new Intent(c, ChatActivity.class);
chat.putExtra(ChatActivity.EXTRA_CHAT_ID, chatId);
PendingIntent intent = TaskStackBuilder.create(c).addNextIntentWithParentStack (chat).getPendingIntent (0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder (c);
final NotificationManagerCompat nm = NotificationManagerCompat.from (c);
builder
    .setSmallIcon (R.drawable.ic_stat_notification)
    .setContentIntent (intent)
    .setGroup (GROUP_KEY_MYAPP)
    .setGroupSummary (true);
    Notification notification = builder.build ();
    nm.notify(NOTIFICATION_ID, notification);

在处理首页时,请从操作栏上点击:

public void onHomeActionDefault (final Activity baseActivity) {
    Keyboard.close (baseActivity);
    Intent upIntent = NavUtils.getParentActivityIntent (baseActivity);
    if (null != upIntent) {
        if (NavUtils.shouldUpRecreateTask (baseActivity, upIntent)) {
            android.support.v4.app.TaskStackBuilder.create (baseActivity)
                                                   .addNextIntentWithParentStack (upIntent)
                                                   .startActivities ();
        } else {
            NavUtils.navigateUpTo (baseActivity, upIntent);
        }
    } else {
        NavUtils.navigateUpFromSameTask (baseActivity);
    }
}

这里有什么我想念的吗?

Is there something I'm missing here?

推荐答案

事实证明,我需要在意图中添加一个标记并使用应用程序上下文.我不需要在目标类内部的java中仅在manifest.xml中创建整个Backstack.因此,我建立了这个小方法来为我正确,一致地构建挂起的意图.

It turns out that I needed to add a flag to the intent and use the application context. I didn't need the whole creation of the backstack in java inside the target class, just in the manifest.xml. So I built this little method to build the pending intents properly and consistently for me.

public static PendingIntent addBackStack(final Context context, final Intent intent) {
   TaskStackBuilder stackBuilder = TaskStackBuilder.create (context.getApplicationContext ());
   stackBuilder.addNextIntentWithParentStack (intent);
   intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
   return stackBuilder.getPendingIntent (0,PendingIntent.FLAG_UPDATE_CURRENT);
}

这篇关于无法从Notification PendingIntent创建Android堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆