2013-03-02 63 views
1

我无法重新启动由PendingIntent启动的一项活动。无法重新启动由PendingIntent启动的活动

“不能重新启动” 没什么意思,对于exmple,的onCreateonNewIntent从未调用时startActivity()有关的活动已经运行

命名MainActivity我不能重新启动的活动是singleTop和覆盖onNewIntent

这里是关于MainActivity的清单。

<activity 
      android:name="com.example.MainActivity" 
      android:label="@string/app_name" 
      android:launchMode="singleTop"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

首先,通过pendingintent通知启动MainActivity。

我的PendingIntent是

Intent iS = new Intent(this,MainActivity.class); 
iS.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendindIntent pi = PendingIntent.getActivity(this, 0, iS, PendingIntent.FLAG_UPDATE_CURRENT); 

而且MainActivity执行startService()和服务执行sendBroadcast(), 和BroadcastClass执行startActivity(Intent(this,MainActivity.class))

这里是BroadcastClass。

public void onReceive(Context context, Intent intent) { 
    Intent i = new Intent(context,MainActivity.class); 

    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(i); 
} 

然后onNewIntent是......没有被调用过onCreate。

但是当通过在homeScreen中按图标启动程序时,MainActivity中的onNewIntent被同样的流程调用。

对不起我的英文不好。 如果你不介意,请告诉我句子修改。

编辑: onNewIntent在这里。

protected void onNewIntent(Intent intent) { 
    Log.d("onNewIntent","onNewIntent"); 
    String SOMETHING = intent.getStringExtra("SOMETHING"); 

    LinearLayout ll = (LinearLayout)findViewById(R.id.ll2); 
    ll.removeAllViews(); 
    TextView textview1 = (TextView)getLayoutInflater().inflate(R.layout.textview, null); 

    textview1.setText(SOMETHING); 
    ll.addView(textview1); 
} 

“onNewIntent”未出现在LogCat上。

+0

您可以发布您的onNewIntent代码吗? – 2013-03-02 14:47:11

回答

1

这是一个bug in Android。另请参阅this

问题是,Android中用于决定是否调用onNewIntent()的代码正在使用Intent中的标志,而不是检查清单中的标志。

开始从广播接收器的活动时,这样你应该能够通过添加Intent.FLAG_ACTIVITY_SINGLE_TOP来解决这个问题:

public void onReceive(Context context, Intent intent) { 
    Intent i = new Intent(context,MainActivity.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    context.startActivity(i); 
} 

它还将帮助,如果你将担任主角,并发表评论,谷歌代码的问题。如果我们做出足够的噪音,这可能会在2020年之前得到修复;-)