简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM