简体   繁体   中英

Clearing getIntent() data when using singleTask activity

My app can receive new files using "file open" intents:

<activity android:launchMode="singleTask" >
    <intent-filter android:label="Text document" android:priority="1">
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="content"/>
        <data android:scheme="file"/>
        <data android:host="*"/>
        <data android:mimeType="*/*"/>
        <data android:pathPattern=".*\\.txt"/>
    </intent-filter>
</activity>

The actual data is processed in the onCreate or onNewIntent methods:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // process intent data using getIntent().getData();
    // When the app is started from a file request then the intent contains the file data.

    // Settings the intent to some dummy data after processing to prevent getting the same data
    // when the task is brought back again from background does not work:
    setIntent(new Intent());
}

When the task is already running then this is called:

@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // process intent data here using intent.getData();
}

All this works fine unless the app is restarted from the Android backgrounded task selection viewer ("recent list of apps"). Each time the app is started from the task selection viewer then the getIntent().getData() is reset to the initial data which causes my app to get the same file data again.

To be more specific these are the steps:

  1. Launch the app by clicking on a *.txt file. onCreate gets the file info.
  2. Press the "back" key to exit the app.
  3. Show the Android task viewer and click on the screenshot of my app. onCreate gets the same file info again. How can this be prevent?

The problem does not occur here:

  1. Launch the app by clicking on a *.txt file. onCreate gets the file info.
  2. Press the "back" key to exit the app.
  3. Now I click on my app's icon in the Android launcher. onCreate does not get the file info again. This is what I want.

Is there a way to clear the getIntent() object so that it is not reset each time my app is created?

Regards,

I found a working solution:

if (((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY)) == 0) {
  // process intent data
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM