Show existing activity from AppWidget click
I have a problem opening an activity from an appwidget. I have tried different Intent flags, PendingIntent and launch modes without any luck. I have read diffent examples here and many other places without finding the solution.
Right now when I click my button on my appwidget it opens a new activity instead of showing the instance which already exists in the app. I have posted my code below and hope you can help me.
Is there a way to find the existing activity and show it instead of creating a new one when clicking my bottom on the appwidget?
Manifest:
<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:n开发者_StackOverflowame="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
</receiver>
</application>
The 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);
}
}
}
set the activity launchMode to singleInstance in manifest.
Let us know if it is working.
Saneesh
Use either FLAG_ACTIVITY_REORDER_TO_FRONT
or the combination of FLAG_ACTIVITY_CLEAR_TOP
and FLAG_ACTIVITY_SINGLE_TOP
. FLAG_ACTIVITY_CLEAR_TOP
without FLAG_ACTIVITY_SINGLE_TOP
would not work, AFAIK.
精彩评论