0

I have an Activity --> A it has two intent filters. when it is called first time the onCreate() method is called. its ok. but when it is already called by a one intent filter and then if it is called by another intent filter the onCreate() method is not called.

this is the Activity tag in manifest:

<activity
        android:name=".login.Login"
        android:configChanges="orientation|keyboardHidden"
        android:windowSoftInputMode="adjustResize"
        android:launchMode="singleTask"
        >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


        <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.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data
                android:scheme="content"
                android:mimeType="application/octet-stream"
                android:pathPattern=".*\\.octopus"
                tools:ignore="AppLinkUrlError" />
        </intent-filter>

    </activity>

This is the onCreate of the Activity:

public void onCreate(Bundle savedInstanceState) {

    if (getResources().getBoolean(R.bool.portrait_only)) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    super.onCreate(savedInstanceState);

// this is true when the second intent filter is used

        Uri data = getIntent().getData();
        if (data != null) {
            getIntent().setData(null);
            try {
                importData(data);
            } catch (Exception e) {
                // warn user about bad data here
                finish();
                return;
            }
        }


......
......

}

1 Answers1

3

You may want to use SingleTop mode of launchMode. By using this you will receive new Intent in onNewIntent(Intent intent) of android Activity Lifecycle method.

In other words, If the activity is already there in the top, and new Intent is starting that activity, then the new Intent will be delivered to the same Activity instance on onNewIntent() method.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • I tried it, but then two app instances are created. the first app instance created is also there. please help – Chathura Karunanayaka Dec 03 '18 at 06:08
  • Use `SingleTop`. You can refer this link to know more about launch modes. https://medium.com/@iammert/android-launchmode-visualized-8843fc833dbe – Paresh P. Dec 03 '18 at 06:14
  • thank you very much. I refered it but now the problem is when the singletop launch mode is set and If it is not on top of task, then new Activity instance will be created. then two same activities are there. that makes the user confused .plese help – Chathura Karunanayaka Dec 03 '18 at 06:24
  • I guess that is the proper way. Because If you use `singleTask`, If A is there, you are starting B and again starting A. New intent will be delivered to A but B will gets destroyed. – Paresh P. Dec 03 '18 at 06:34