2011-09-29 33 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。