Android 活动不是';我没有发现新的意图

Android 活动不是';我没有发现新的意图,android,Android,出于各种原因,“我的应用”会在通知栏中创建条目。当用户单击通知时,我想显示一个自定义活动,以特定格式显示条目 Intent intent = new Intent(applicationContext, TextMessageViewerActivity.class); intent.putExtra("text_message", someText); PendingIntent contentIntent = PendingIntent.getActivity(applicationCont

出于各种原因,“我的应用”会在通知栏中创建条目。当用户单击通知时,我想显示一个自定义活动,以特定格式显示条目

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
这一切归结为通过一个额外的意图发送一个字符串,并在我的活动中显示它。发生的情况是,传递给我的活动的第一个意图以某种方式被“卡住”,而传递给它的所有进一步的意图只显示第一个意图的额外部分

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活动生命周期,我还不了解什么?

我认为您需要使用
onNewIntent()
函数。它会改变您的活动看到的意图。用法:

/**
  * onResume is called immediately after this.
  */
@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    resolveIntent(intent);
}

这里
resolveentent
用于实际处理新的传入意图。查看文档。

我今天学到了一些新东西:对于给定的
活动
,可以返回相同的
挂起内容。要为每个要发送的唯一
目的
创建唯一的
pendingent
,必须在创建时指定
pendingent.FLAG_ONE_SHOT
标志

例如,上面代码中的PendingEvent创建行应该是:

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

这实际上是我一开始的想法,但除非为活动指定“singleTop”,否则不会调用onNewIntent。不过,这是一个直观的答案。