2011-09-29 35 views
0

从appwidget打开活动时出现问题。我尝试过不同的Intent标志,PendingIntent和启动模式,没有任何运气。我在这里和其他许多地方阅读过不同的例子,但没有找到解决方案。从AppWidget显示现有活动点击

现在,当我点击我的appwidget上的按钮时,它会打开一个新的活动,而不是显示应用中已经存在的实例。我在下面发布了我的代码,希望你能帮助我。

有没有办法找到现有的活动,并显示它,而不是创建一个新的appwidget时单击我的底部?

清单:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" android:name="widget.helper.ResourceHelper"> 
    <activity android:name=".ScoreBoard" 
       android:label="@string/app_name" 
       > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" />         
     </intent-filter>   
    </activity> 


    <!-- Broadcast Receiver that will process AppWidget updates --> 
    <receiver android:name="Widget" android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 
     <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" /> 
    </receiver>  
</application> 

的AppWidget:

public class Widget extends AppWidgetProvider { 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     RemoteViews remoteViews; 
     ComponentName thisWidget; 
     remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); 
     thisWidget = new ComponentName(context, Widget.class); 
     appWidgetManager.updateAppWidget(thisWidget, remoteViews); 
    } 

    @Override  
    public void onReceive(Context context, Intent intent) 
    { 
     super.onReceive(context, intent); 

     if(intent.getAction().equals("OPEN_APP")) { 
       Intent i = new Intent(Intent.ACTION_MAIN); 
      i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      i.setComponent(new ComponentName("widget","widget.ScoreBoard")); 
      ResourceHelper.getScoreBoard().startActivity(i); 
     } 


    } 
} 

回答

0

将清单中的activityMode设置为singleInstance。

让我们知道它是否工作。

Saneesh

0

选其一FLAG_ACTIVITY_REORDER_TO_FRONT组合FLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_SINGLE_TOPFLAG_ACTIVITY_CLEAR_TOP没有FLAG_ACTIVITY_SINGLE_TOP不会工作,AFAIK。