获取消息对象PendingIntent,获取null Intent.getExtras()。getString(" mystring")

时间:2017-06-05 12:52:38

标签: android android-fragments android-intent android-activity

我在点击底栏时在活动中调用片段。 片段在片段中有以下消息对象。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

      Intent moreSelectionIntent = new Intent(getActivity(), MainActivity.class);
      moreSelectionIntent.putExtra("selection",Integer.toString(position));
     PendingIntent pendingIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), 0, moreSelectionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
     }        ` 

片段调用activity并传​​递PendingIntent。当我使用下面的代码从意图中获取字符串时,我在活动中得到NULL 下面是活动中的一段代码

 Intent i = this.getIntent();
    Log.i(TAG, "intenet------->"+i);

    String moreSelection = i.getExtras().getString("selection");
    Log.i(TAG, "moreSelection Bundle------->"+moreSelection);

    if(moreSelection!=null)
    { System.out.println("message"+moreSelection);}

2 个答案:

答案 0 :(得分:0)

您必须添加额外的字符串才能获得它。首先将它添加到pendingIntent。

答案 1 :(得分:0)

使用此代码...

    Intent intent = new Intent(this, NextActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
    PendingIntent.FLAG_ONE_SHOT);