Search code examples
androidtwittertwitter-oauthflagsscribe

How to remove browser from background after twitter authentication?


In my android application we are using Scribe API for Twitter login. For calling twitter authentication url in browser using below code

OAuthService service  = new ServiceBuilder()
    .provider(TwitterApi.SSL.class)
    .apiKey(myApiKey)
    .apiSecret(myApiSecret)
    .callback(twitterCallback)
    .build();
Token  token = service.getRequestToken();
String authUrl = service.getAuthorizationUrl(token);

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setData(Uri.parse(authUrl));
context.startActivity(intent);

After twitter authentication has completed coming back to activity now we are moving to another activity in the application i.e MainActivity.

@Override
protected void onResume() {
super.onResume();
if(uri != null && uri.toString().startsWith(“oauth://twitter”){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        } 
}

Manifest code is given below

<activity
        android:name=".LauncherActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="twitter"
                android:scheme="oauth" />
        </intent-filter>
    </activity>

In Main activity we have logout button, When i press logout here calling below method,

private void logout(){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        this.finish();
}

So far working fine,Now going to device Homescreen. Here the problem is when i long press on home button showing two apps one is my native application and another one is myapp in browser. How to handle remove browser after authentication has completed?

Thanks in advance


Solution

  • Finally got solution, add one flag to intent which is before opening in browser Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS. Now its working perfetct. code is

       Intent intent = new Intent(Intent.ACTION_VIEW);
       intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS ); 
       intent.setData(Uri.parse(result));
       context.startActivity(i);
    

    Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS reference here http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS