问题
I have included the AccountManager code in my app to enable the user to create and manage their account from inside their Settings application.
I linked in the "accountPreferences" preferences file inside my account-authenticator definition, and the options show properly in the Settings > Accounts > My App screen. When I tap on one of them, instead of getting the activity I linked in, I get:
01-14 14:47:27.304: ERROR/AndroidRuntime(27708): FATAL EXCEPTION: main
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:944)
at android.app.ContextImpl.startActivity(ContextImpl.java:931)
at android.preference.Preference.performClick(Preference.java:967)
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)
I have the intents inside my PreferenceScreen defined as:
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.myapp.android"
android:targetClass="com.myapp.android.activities.AccountForwardingActivity"/>
And the target activity is also defined as you'd expect, with no special flags other than an intent-filter to match that action (I have other custom actions listed as well).
<activity android:name=".activities.AccountForwardingActivity"
android:theme="@style/Theme.MyApp"
android:launchMode="singleTask">
<intent-filter>
<action android:name="com.myapp.android.PAYMENT_TYPES"/>
<action android:name="com.myapp.android.ADDRESS_LIST"/>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
What am I doing wrong? How can I mark this intent, launched from Settings.apk, to be a new task?
回答1:
My problem was putting the Preference Screens inside my PreferenceCategory.
This works fine:
<PreferenceCategory android:title="General Settings" />
<PreferenceScreen
android:title="Account Settings"
android:summary="Favorites, Orders, and more.">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.myapp.android"
android:targetClass="com.myapp.android.activities.AccountForwardingActivity"/>
</PreferenceScreen>
Nesting that PreferenceScreen inside the PreferenceCategory does not work, and results in the error above.
来源:https://stackoverflow.com/questions/14328253/launch-intent-from-account-authenticator-accountpreferences-screen