Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirecting to Android app from browser

I am working on a Android app at present that request information from a server via an HTML request and gets a params string in return. This happens during the startup process in order to configure the app the first time it is run.

To send the request to the server I call the following from the OnCreate method of the main activity of my app:

String URL = "http://myserver.mydomain.com/users/start";            
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
startActivity(browserIntent);

The server, a rails app, gets the required parameter values and redirects back to the app with the following statement:

redirect_to "http://myappname.mydomain.com/?#{mobile_params.to_query}"

I have created an intent filter in my AndroidManifest.xml for the activity that handles the parameters that come back from the server. The intent filter looks like:

    <activity
        android:name=".StartFromUriActivity"
        android:launchMode="singleTop"
        android:noHistory="true"
        android:screenOrientation="behind"
        android:theme="@style/NoTitle" 
        android:exported="true" >
        <intent-filter>
            <data android:scheme="http" android:host="myappname.mydomain.com" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
             <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    </activity>

Finally, the first few lines of my StartFromUriActivity.java look like this:

public class StartFromUriActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Uri URIdata = getIntent().getData();
        if(URIdata != null) {
            parameter1value = URIdata.getQueryParameter("param1");
        }
    finish();
    }
}

All of this actually works fine, with one exception. The app opens a browser, sends the URI to the server, which redirects back to the app with the desired parameter values, the intent filter directs to the activity and the parameters are read correctly. The only problem is that the browser window is left in the foreground and I have to manually close it. I have tried everything I can think of to get my app to come to the foreground and for the browser to close or go to the background without success. What am I missing that would result in my app correctly being brought to the foreground?

like image 448
Blaze Avatar asked Aug 05 '13 15:08

Blaze


People also ask

Can I open Android app from URL?

You achieve this by Deep linking in your app. First of all you need to add intent filters for incoming links. Specify the ACTION_VIEW intent action so that the intent filter can be reached from Google Search. Add one or more tags, each of which represents a URI format that resolves to the activity.


1 Answers

I tried dsschnau's suggestion of adding android:allowTaskReparenting="true" to the activity in AndroidManifest.xml. This didn't solve the problem, so I tried adding android:parentActivityName=".MainActivity" and android:taskAffinity="myapp" so that reparenting had a explicit target, but still no luck.

I then realized that the problem was that the finish() statement in StartFromUriActivity.OnCreate was ending the activity and it was correctly returning to the next activity in the stack, the browser. Solution was to add the following lines prior to the finish() statement.

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
like image 128
Blaze Avatar answered Sep 26 '22 04:09

Blaze