Brandon
Brandon

Reputation: 1427

How to stop my activity from opening another instance in another application from share menu?

I have my application that allows other apps that can share files to select it on the share menu, open the application up and "transfer" the files over to my app.

My problem is when they select my application from the share menu (as example, in gallery, after selecting several photos, they choose to share with my app) the login activity gets opened inside the gallery application

Please note that i have tried this process works on older devices and it worked but not for a newer device.

This is my manifest.xml

<application
    android:name="com.Application"
    android:allowBackup="true"
    android:fullBackupContent="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:mimeType="*/*"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustPan"/>
    <activity
        android:name=".login.LoginActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:windowSoftInputMode="adjustResize|stateVisible">
        <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.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="*/*" />
        </intent-filter>
    </activity>
</application>

I have two activities for this app, login and Main, Main will be loaded up if login is successful.

I have tried with different kinds of launcherMode settings but nothing has stopped the instance from being created in gallery

This is from my login Activity

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

    setContentView(R.layout.activity_login);
    receivedIntent = getIntent();
    //set orientation
    setRequestedOrientation(isPhone() ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent != null && intent.getAction() != null && (intent.getAction().equals(Intent.ACTION_SEND) || intent.getAction().equals(Intent.ACTION_SEND_MULTIPLE))){
        finish();
        startActivity(intent);
    }
}

The problem is, on the older device the onNewIntent method gets called as expected, but on the newer device it just ignores the launcherMode and creates the new instance and runs onCreate()

Any ideas on how to get the application to "force" launch itself when called from a share menu?

Current Testing devices - Samsung mobile Android 8.0, API 26 - not working Samsung Tablet Android 7.1.1, API 25 - working as expected

Upvotes: 0

Views: 319

Answers (2)

Brandon
Brandon

Reputation: 1427

I did some digging into other apps on the device and found this issue is only occurring when sharing images from the application "Gallery" otherwise the code works as expected, so i believe this could be an issue with Gallery itself or is expected behavior for the Gallery application.

Upvotes: 0

Rushikesh Pundkar
Rushikesh Pundkar

Reputation: 51

All your issue is about the permissions, Old devices below kitkat(4.4.2) does not required permissions of file but in new devices required permissions for access the file data(ie. gallery and others). You have to grant permission programmatically,

if (Build.VERSION.SDK_INT > 19) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            arrPerm.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (!arrPerm.isEmpty()) {
            String[] permissions = new String[arrPerm.size()];
            permissions = arrPerm.toArray(permissions);
            ActivityCompat.requestPermissions(this, permissions, MY_PERMISSIONS_REQUEST);
        } 
    }

After Permission Granted you can do your operation:

if (Build.VERSION.SDK_INT > 19) {
                    if (isPermissionGranted()) {
                        //Do your Task of gallery
                    } 
                }

Or alternate way, you can use library directly: https://github.com/nabinbhandari/Android-Permissions

Upvotes: 0

Related Questions