Android: SingleTop behavior with multiple intent filters
-
21-12-2019 - |
Question
I am struggling with the following strange behavior of SingleTop activities.
I have defined some intent filters in that activity:
<activity android:name="com.example.DashboardActivity"
android:screenOrientation="landscape"
android:launchMode="singleTop"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="video" />
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.EDIT" />
<data android:scheme="survey" />
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CALL" />
<data android:scheme="call" />
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This code should start the activity:
webView.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
} catch(ActivityNotFoundException e) {
Log.e(TAG,"Could not load url"+url);
}
return super.shouldOverrideUrlLoading(view, url);
}
});
in the onResume of the DashboardActivity I check for the according action:
public void onResume(){
super.onResume();
Fragment fragment = null;
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
extras.putParcelable("uri", getIntent().getData());
fragment = new VideoplayerFragment();
} else {
fragment = new DashboardFragment();
}
addOrReplaceFragment(fragment, extras);
}
But when I run this code, I always get the android.intent.action.MAIN
action. If I remove the singleTop
launch mode, it launches a new activity, but passes the correct intent. I have to use the same instance of the activity, so singleTop
, singleTask
or singleInstance
must do the job. But I don't know what's going wrong here. HELP!
Solution
Intent.ACTION_VIEW.equals(intent.getAction())
Where is this intent coming from? In order to catch the new Intent you must use onNewIntent().
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow