Abdul Fatir
Abdul Fatir

Reputation: 6357

Activity being recreated even after setting launchMode="singleTop"

I was implementing OAuth for Fitbit using a custom scheme and for that I require the instance of the calling Activity to be used from the top of stack after the user logs-in in the default browser and is redirected to the CALLBACK URL. Instead of onNewIntent() being called, the Activity is just recreated which is not what I require.

<activity
        android:name=".AppsAndDevicesActivity"
        android:launchMode="singleTop">
        <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:scheme="snbr" />
        </intent-filter>
    </activity>  

I'm opening the browser from a Fragment using the following code:

String url = Fitbit.buildAuthenticationURL();
Log.d("URL", url);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
this.getActivity().startActivity(browserIntent);

Upvotes: 3

Views: 1530

Answers (1)

santoXme
santoXme

Reputation: 802

use launchmode as singleTask because:

singleTask:

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

Note: Although the activity starts in a new task, the Back button still returns the user to the previous activity.

Upvotes: 6

Related Questions