开发者

Android - How to launch an activity from a widget and return to home screen

I have a widget that when clicked opens an activity from same app as the widget. When the activity is closed/dismissed via a button, the user will see the full app window IF the app was previously open/in memory. Is there a way for the activity to finish and return to the home screen and not to an existing instance of the app?

Intent i = new Intent(this,RateIt.class);
i.putExtra("com.sporadicsoftware.NetQ.movie_id",aMovie.title_id);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_NO_HISTORY|Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(context,
                    0, i, 开发者_JS百科PendingIntent.FLAG_UPDATE_CURRENT);
            updateViews.setOnClickPendingIntent(R.id.movie_one_title, pendingIntent);


Take a look at the UI Guideline "Notifications should let the user easily get back to the previous activity" for a similar situation as well as the Activities and Tasks section of the Application Fundamentals framework topic.

Depending on your needs, you may be able to use intent flag FLAG_ACTIVITY_NEW_TASK in combination with setting an individual affinity for the activity you want to open separately. You can set the taskAffinity attribute of the <activity> element in your manifest to the empty string. This would only be a good solution if you always only use this activity separately from the rest of the application.

From the documentation:

android:taskAffinity

The task that the activity has an affinity for. Activities with the same affinity conceptually belong to the same task (to the same "application" from the user's perspective). The affinity of a task is determined by the affinity of its root activity. The affinity determines two things — the task that the activity is re-parented to (see the allowTaskReparenting attribute) and the task that will house the activity when it is launched with the FLAG_ACTIVITY_NEW_TASK flag.

By default, all activities in an application have the same affinity. You can set this attribute to group them differently, and even place activities defined in different applications within the same task. To specify that the activity does not have an affinity for any task, set it to an empty string.

If this attribute is not set, the activity inherits the affinity set for the application (see the <application> element's taskAffinity attribute). The name of the default affinity for an application is the package name set by the <manifest> element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜