Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreferenceScreen - <intent .../> - Exception - FLAG_ACTIVITY_NEW_TASK

I am new at android developing.

The project is about implementing an AbstractAccountAuthenticator with an AbstractThreadedSyncAdapter to sync certain data on a server with a content provider.

I made everything and both adding accounts and syncing is running without any problems.

Now I've tried to add a Preference-Screen just like suggested in the android reference (search for AbstractAccountAuthenticator, it is explained there) via xml like this:

autenticator.xml:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="typeOfAuthenticator"
    android:icon="@drawable/icon"
    android:smallIcon="@drawable/miniIcon"
    android:label="@string/label"
    android:accountPreferences="@xml/account_preferences"
 />

The Preference-Screen-xml looks like this:

account_preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory android:title="@string/pref_cat_general">
  <PreferenceScreen android:key="@string/pref_cat_scr_keygen" android:title="@string/pref_cat_scr_keygen_title" android:summary="@string/pref_cat_scr_keygen_summary">
   <intent android:action="android.intent.action.VIEW" android:targetPackage="com.android.clean" android:targetClass="com.android.clean.KeygenAct" />
  </PreferenceScreen>
 </PreferenceCategory>
</PreferenceScreen>

The screen is promted how it should be, but and that is the problem: When I click on the PreferenceScreen with the intent it leads to a crash of the system (both in the emulator and my HTC-Desire).

Logcat says:

ERROR/AndroidRuntime(18754): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

(whole Logcat on pastebin)

Here the part of the manifest, where the activity is defined:

<activity android:name=".KeygenAct">
    <intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

The whole test-code (it is my project on the essentials to find the problem) is to find at googlecode (http://code.google.com/p/cleanproject/source/browse/) (Eclipse-Project)

Thank you already for your help, Esentian

p.s.: keygen is not a key generator, it means more key_general. A bit inartfully expressed ;)

like image 936
Esentian Avatar asked Sep 16 '10 13:09

Esentian


People also ask

What is Flag_activity_new_task?

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

What are the three pieces of information in the intent that are used for resolution describe each of them?

There are three pieces of information in the Intent that are used for resolution: the action, type, and category. Using this information, a query is done on the PackageManager for a component that can handle the intent.

What are intent flags in Android?

Use Intent Flags Intents are used to launch activities on Android. You can set flags that control the task that will contain the activity. Flags exist to create a new activity, use an existing activity, or bring an existing instance of an activity to the front.

How do I set extras intent?

Use the Intent bundle to add extra information, like so: Intent i = new Intent(MessageService. this, ViewMessageActivity. class); i.


1 Answers

i had the same problem and have just found out, that if you change your preferences to:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory android:title="@string/pref_cat_general">
 </PreferenceCategory>
  <PreferenceScreen android:key="@string/pref_cat_scr_keygen" android:title="@string/pref_cat_scr_keygen_title" android:summary="@string/pref_cat_scr_keygen_summary">
   <intent android:action="android.intent.action.VIEW" android:targetPackage="com.android.clean" android:targetClass="com.android.clean.KeygenAct" />
  </PreferenceScreen>
</PreferenceScreen>

Then it works as expected. This is weird because I would expect PreferenceCategory to group items as its children... And why does it result with such unrelated (?) error?

like image 62
jan Avatar answered Sep 20 '22 00:09

jan