How to clear intent data in Activity after opening from URL?

I have an Activity that can be launched directly from the browser by calling url.

Operation code:

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Uri data = getIntent().getData(); Log.d(getClass().getName(), "onCreate data=" + data); getIntent().replaceExtras(new Bundle()); getIntent().setAction(""); getIntent().setData(null); getIntent().setFlags(0); if (data != null && isValidUrl(data)) { // open some special fragment } else { // open main fragment } } @Override protected void onDestroy() { super.onDestroy(); Log.d(MainActivity.class.getName(), "onDestroy"); } //... } 

Application manifest code:

  ... <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:host="example.com" android:pathPrefix="/" android:scheme="http"/> </intent-filter> </activity> ... 

This code works just fine. When I open the application from the browser using the link ( http://example.com/somepage.html ), I have this output:

 D/com.package.MainActivity: onCreate data=http://example.com/somepage.html 

But if I leave the application (using the "Back" button) and open the application again, from the last menu I will get the same result:

 D/com.package.MainActivity: onDestroy D/com.package.MainActivity: onCreate data=http://example.com/somepage.html 

I want to clear the intent data in the onCreate method. Or is there a way to detect when an application starts from a recent menu?

UPD: I tried to do this:

 @Override protected void onDestroy() { super.onDestroy(); getIntent().replaceExtras(new Bundle()); getIntent().setAction(""); getIntent().setData(null); getIntent().setFlags(0); Log.d(MainActivity.class.getName(), "onDestroy"); } 

But that does not help.

+6
source share
6 answers

Remove aim.getData () on the lower code:

Change 1:

 Uri data = getIntent().getData(); Log.d(getClass().getName(), "onCreate data=" + data); if (data != null && isValidUrl(data)) { // open some special fragment } else { // open main fragment } getIntent().setData(null); 

Edit 2:

 getIntent().replaceExtras(new Bundle()); getIntent().setAction(""); getIntent().setData(null); getIntent().setFlags(0); 

Hope this helps you.

+3
source

The way you want to do this is contrary to the principles of the Android system. Android is designed in such a way that Intent opens an Activity , and then if you want to open this Activity from the "Recursors" menu, it gets the same intention.

I just guess that if you use the one-Activity-many-fragments approach, then you see one of its drawbacks. Android Intent is designed to take advantage of the One Screen - One Activity approach, which is best practice.

If I am mistaken, and you have another case, it would be really interesting to find out. Then you can think about how to change the application to meet your needs.

+2
source

Instead of changing the intent data, you can save the intent hash and compare this hash every time the application receives focus. This way you will see if a new intention is present.

Example:

 int currentIntent = getIntent().hashCode(); if (lastIntent != currentIntent) { // do stuff } 
0
source

In your onCreate method you have

 if (data != null && isValidUrl(data)) { // open some special fragment } else { // open main fragment } 

Change to

 if (data != null && isValidUrl(data)) { // open some special fragment getIntent().setData(null); setIntent(getIntent()); //This will set the new intent to have no data, //so future launches won't relaunch the data url. } else { // open main fragment } 
0
source

Pretty old thread here, but if someone is looking for this:

You can check if your intention was sent due to the launch of the application from the last list by checking the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY flag

  if ((getIntent().getData() != null) && ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0)) { //Handle the url passed through the intent } else { //proceed as normal } 
0
source

You can call the following Intent cleanup method.

 getIntent().removeExtra("key"); 

Just replace the "key" with the name Intent passed from the source.

-1
source

All Articles