Best Practice for Starting Activities Outside an Activity Context?

5,777 views
Skip to first unread message

Heath

unread,
Feb 18, 2011, 9:47:39 AM2/18/11
to stl-mob...@googlegroups.com
In my app, I have an Application Context that stores my WebView which I use to make requests on behalf of the user.  This class also functions as my app's top-level controller since it has to present different views based on events from the WebView.  The natural way to present these views is to use Intents.  However, since my WebView exists in the ApplicationContext, I don't have an ActivityContext with which to start an Activity.

The Exception I received says if I want to start a new Activity this way, I need to start a new task as well.  This doesn't sound like the best thing to do.  Also, I considered just using implicit IntentFilters to launch my activities, but I'd prefer to use explicit IntentFilters since these views are strictly internal to my app.

I also considered passing the Activity Context to my Application Context to use in my explicit IntentFilter, but that seems like a hack.  It also sounds like the lifecycle offered by services.

Does anyone have a best practice they could point me to?

Thanks!

Andrew Prunicki

unread,
Feb 18, 2011, 9:54:41 AM2/18/11
to stl-mob...@googlegroups.com
Did you try clear top?


It may still result in a new task, however.

You are right, launching an activity in this way is just like from a service.

Eric Burke

unread,
Feb 18, 2011, 10:00:51 AM2/18/11
to stl-mob...@googlegroups.com, Heath
You need the FLAG_ACTIVITY_NEW_TASK on the Intent.

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK

The name is a bit of a misnomer. If a task doesn't exist, it will create one. If the task is already there, the activity joins up with the existing task. A "task" is just a stack of activities.

You may also have to tweak your AndroidManifest.xml, as well as combine that flag with other flags like FLAG_ACTIVITY_CLEAR_TOP.

We have an Activity in Square that can be launched explicitly from another Activity, or it can be launched from an item in Android's notification area. Here is a snippet from our AndroidManifest.xml:

    <!--
      - Shows ***redacted*** from '***' as well as enqueued items.
      - singleTop launch mode ensures we don't get a stack of duplicate
      - activities when this is launched repeatedly from Android's notification
      - area.
      -->
    <activity
        android:name="com.squareup.ui.MyActivity"
        android:launchMode="singleTop"
        />

When we launch MyActivity from another Activity, it's done normally:

startActivity(new Intent(this, MyActivity.class));

But when we launch MyActivity from outside an Activity context, we set some flags:

    // FLAG_ACTIVITY_NEW_TASK is required. Without it, Android automatically
    // adds that flag and logs a warning on every usage.
    // FLAG_ACTIVITY_CLEAR_TOP brings the xxxx activity back to
    // the top of the stack if it had launched child activities.
    Intent intent = new Intent(context, MyActivity.class);
    intent.putExtra(EXTRA_BACK_TEXT_ID, R.string.back);
    intent.putExtra(EXTRA_VIA_NOTIFICATION, true);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
        Intent.FLAG_ACTIVITY_CLEAR_TOP);
--
Eric M. Burke
636-542-8753 (Google Voice)

Heath Borders

unread,
Feb 18, 2011, 10:16:31 AM2/18/11
to stl-mob...@googlegroups.com

Thanks guys.  Are there any opinions about doing this as a service instead? A service seems better since it retains the interested context. It feels a little hackish to rely on the state of your application when a method is called rather than having that state pssed to you explicitly.

Thanks for the quick feedback!

-Heath

> In my app, I have an...

Reply all
Reply to author
Forward
0 new messages