I have an android app that register a custom url scheme (let’s say app://)
In the onResume() method of my main activity I get data from the url by calling getIntent().getData().
When I browse to a web page containing such a link (like app://action/1234) then getIntent().getData() return the info I need to take appropriate action. That’s fine.
Now I want to have a WebView in my app that does the same. Here’s my code :
WebView banner = ...
banner.setWebViewClient(new WebViewClient() {
@Override
public void onLoadResource(WebView view, String url) {
if (url.startsWith("app://")) {
try {
Intent i = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
startActivity(i);
return true;
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}
}
When I click on the link in the WebView, the onResume() method of the main activity is called BUT getIntent().getData() returns null.
I guess
Intent i = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
startActivity(i);
is not the good way to do it.
How should I do to reproduce the same event that happens when I click a link in the browser ?
EDIT:
My activity has android:launchMode="singleTop" and it seems to be the cause of my problem but if I don’t set the launch mode then another copy of the activity is launched (I don’t want that). I tried to handle the intent by overriding onNewIntent() and in the intent given to it I can see the data. But I want my app to react in the same manner with it’s launched from its inside or from another app then
Is it safe to do this ? :
@Override
public void onNewIntent(Intent intent) {
getIntent().setData(intent.getData());
}
should do it.