活动没有拿起新意图 [英] Activity isn't picking up new intent

查看:94
本文介绍了活动没有拿起新意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于种种原因,我的应用程序会在通知栏条目。当用户点击该通知,我要显示一个定制的活动,presents特定格式的条目。

For various reasons, my app creates entries in the notification bar. When the user clicks on the notification, I want to display a custom activity that presents the entry in a certain format.

Intent intent = new Intent(applicationContext, TextMessageViewerActivity.class);
intent.putExtra("text_message", someText);
PendingIntent contentIntent = PendingIntent.getActivity(applicationContext, 0, intent, 0);
// now create a notification and post it. no fancy flags

这一切都归结于通过一个额外的意向发送一个字符串,并显示在我的活动。什么情况是,该被送到我的活动的第一个意图被卡住不知何故,并交付给它的所有进一步意图显示只有第一个意图的额外费用。

It all boils down to sending a string via an Intent extra, and displaying that in my Activity. What happens is that the first Intent that gets delivered to my Activity gets "stuck" somehow, and all further Intents delivered to it display the only the first Intent's extra.

public class TextMessageViewerActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_viewer);
    }

    @Override
    public void onResume()
    {
        super.onResume();

        Intent startingIntent = getIntent();
        String message = startingIntent.getStringExtra("text_message");
        String displayMessage = message != null ? message : "No message found";

        ((TextView)findViewById(R.id.text_viewer_text_id)).setText(displayMessage);
    }
}

我是什么不理解有关Android的活动周期?

What am I not understanding about the Android Activity lifecycle?

推荐答案

我今天新学到了一些东西:<一href=\"http://developer.android.com/reference/android/app/PendingIntent.html#getActivity%28android.content.Context,%20int,%20android.content.Intent,%20int%29\"相对=nofollow> PendingIntent.getActivity 可以返还相同的PendingIntent 对于给定的活动。为了创造一个独特的的PendingIntent 为每个唯一意图发送,您必须指定 PendingIntent.FLAG_ONE_SHOT 标记创建时。

I learned something new today: PendingIntent.getActivity may return the same PendingIntent for a given Activity. In order to create a unique PendingIntent for each unique Intent to send, you must specify the PendingIntent.FLAG_ONE_SHOT flag when creating it.

因此​​,例如,在我的code中的创造的PendingIntent线以上应该是:

So for example, the PendingIntent creation line in my code above should have been:

PendingIntent contentIntent = PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);

这篇关于活动没有拿起新意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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