Android supports simultaneous permissions from different sources

I am trying to request multiple permissions at the same time, based on multiple sources. My problem is that only one permission is requested.

Here's an example:

We have 2 Fragments that are simultaneously loaded into MainActivity, for example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.MainActivity"
    android:orientation="vertical">

    <fragment android:name="com.example.myapplication.fragments.FragmentOne"
        android:id="@+id/fragment_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <fragment android:name="com.example.myapplication.fragments.FragmentTwo"
        android:id="@+id/fragment_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

      

We have a FragmentOne that asks for READ_CALL_LOG, for example

@Override
public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart: ");
    requestPermissions(new String[]{Manifest.permission.READ_CALL_LOG}, 1);
}

      

We also have a FragmentTwo that asks for READ_CONTACTS, for example

@Override
public void onStart() {
    super.onStart();
    Log.d(TAG, "onStart: ");
    requestPermissions(new String[] { Manifest.permission.READ_CONTACTS }, 2);
}

      

This means that immediately after starting the application, 2 fragments are created and then launched. When this happens, both fragments request permission at the same time. My problem is that only the first permission is always asked for the user.

Now, to figure out my problem, I have added some logs to onRequestPermissionsResult for each snippet like

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    Log.d(TAG, "onRequestPermissionsResult: requestCode=" + requestCode + " permissions=" + permissions
            + " permissions.length=" + permissions.length + " grantResults.length=" + grantResults.length);
    for (String permission : permissions) {
        Log.d(TAG, "onRequestPermissionsResult: permission=" + permission);
    }
}

      

After that I found my problem but was left without a solution. When I looked at the logs, as soon as I run the application I get these logs

2017-05-02 13:22:40:0356    + 677   05-02 13:22:38.650 27003 27003 D FragmentOne: onStart:
2017-05-02 13:22:40:0357    + 699   05-02 13:22:38.661 27003 27003 D FragmentTwo: onStart:
2017-05-02 13:22:40:0357    + 702   05-02 13:22:38.661 27003 27003 D FragmentTwo: onRequestPermissionsResult: requestCode=2 permissions=[Ljava.lang.String;@afbcb45 permissions.length=0 grantResults.length=0

      

At the same time, a dialog box with a READ_CALL_LOG request is displayed. The problem here is that it enters the onRequestPermissionsResult too early.

As soon as I accept the READ_CALL_LOG permission from the dialog, I get 2 new logs

2017-05-02 13:27:42:0017    + 2803  05-02 13:27:40.358 27003 27003 D FragmentOne: onRequestPermissionsResult: requestCode=1 permissions=[Ljava.lang.String;@5028ba8 permissions.length=1 grantResults.length=1
2017-05-02 13:27:42:0017    + 2804  05-02 13:27:40.358 27003 27003 D FragmentOne: onRequestPermissionsResult: permission=android.permission.READ_CALL_LOG

      

At this point, the second permission window (for READ_CONTACTS) that I was expecting is NOT displayed. No more logs are created.

Now I know that I can get all permissions from different fragments and immediately request all of them by passing in the appropriate String array. But that's not what I'm trying to do here. I am trying to have multiple components handle their own permissions.

Am I missing something? Am I doing something wrong?

For reference, I'll put the complete code for MainActivity and FragmentOne here. FragmentTwo is pretty much the same (only TAG and resolution change).

MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

      

FragmentOne:

public class FragmentOne extends Fragment {

    private static final String TAG = "FragmentOne";

    public FragmentOne() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_one, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: ");
        requestPermissions(new String[]{Manifest.permission.READ_CALL_LOG}, 1);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.d(TAG, "onRequestPermissionsResult: requestCode=" + requestCode + " permissions=" + permissions
                + " permissions.length=" + permissions.length + " grantResults.length=" + grantResults.length);
        for (String permission : permissions) {
            Log.d(TAG, "onRequestPermissionsResult: permission=" + permission);
        }
    }
}

      

+3


source to share


1 answer


Try putting permission requests in an action instead of a fragment and requesting both at the same time. The problem seems to be that when you ask for the second permission, the first dialog is closed, so it is treated as a rejection.



0


source







All Articles