Search code examples
javaandroidandroid-studio

How can we extract values from URL when redirect to a Android app?


Currently, I am using this link

https://developer.android.com/studio/write/app-link-indexing#java

To understand the support links in an Android app. Now if the supported link in like this

https://app.example.com?refer=A91NB

or

https://app.example.com/A91NB

How can we extract the refer value from the URL when it's redirected to the app.


Solution

  • Uri uri = getIntent().getData();
    
    if (uri != null) {
        List<String> params = uri.getPathSegments();
        String id = params.get(params.size() - 1);
        Toast.makeText(MainActivity.this, "ID :" + id, Toast.LENGTH_LONG).show();
    }
    

    Write this code in the activity into which your link is being redirected to.

    The ".get(params.size() - 1" will fetch the data for the last segment.

    For eg. https://app.example.com/A91NB: It will give you =>

    A91NB

    You can modify the above code according to the placement of the required data in the link.