0

I am currently using a Viewpager implementation in my android app. In each of the fragment, there is a button that takes a photo and uploads it to an online database. However, due to the new permissions requirement, I needed to ask for permission. Hence, i would need to use the onRequestPermissionsResult() to continue uploading.

Below is my implementation

this is from the ViewPager.class (Since after permission is granted, this method in the Viewpager is called instead of the one in the fragment)

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_TAKE_PHOTO:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Permission granted to write and read file
                //Get the current fragment
                List<Fragment> fragments = getSupportFragmentManager().getFragments();
                for (Fragment currFragment : fragments) {
                    if (currFragment != null)
                        currFragment.onRequestPermissionsResult(
                                REQUEST_TAKE_PHOTO
                                , permissions
                                , grantResults);
                }
            }
            return;
        default: //Do nothing
            return;
    }
}

this is from the fragment itself

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_TAKE_PHOTO:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //Permission granted to write and read file
                uploadImage();
            }
            return;
        default: //Do nothing
            return;
    }
}

this part

List<Fragment> fragments = getSupportFragmentManager().getFragments();
                    for (Fragment currFragment : fragments) {
                        if (currFragment != null)
                            currFragment.onRequestPermissionsResult(
                                    REQUEST_TAKE_PHOTO
                                    , permissions
                                    , grantResults);
                    }

is my attempt to find the correct fragment that clicked the button and call the OnRequestPermissionResult() method.

However, I notice this weird behavior from Viewpager. The way I implemented it is that I click a Viewholder from a RecyclerView, then that enters that position in the ViewPager.

The weird behavior is that for example, I entered page 2 from the RecyclerView. Then i swipe to page 4 and click the upload photo button, after the photo is done(via the in-build camera app), it will switch back to page 2.

Why is that? How do I fix it? Sorry for a lengthy post. Just trying to get as much information out there. Thanks

EDIT: This is how I call the requestPermission

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
        return;
    }
    if (requestCode == REQUEST_TAKE_PHOTO) {
        //Request for permission to access file
        if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            //ask for permission
            ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    REQUEST_TAKE_PHOTO);
        } else {
            uploadImage();
        }
    }
}

Is something wrong?

Edit Post: This is the part where I called the notifyDataSetChange()

private void loadData() {
    mRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<RouteInstructions> mList2 = new ArrayList<RouteInstructions>();

            for (DataSnapshot instructions : dataSnapshot.getChildren()) {
                mList2.add(instructions.getValue(RouteInstructions.class));
            }
            mList = mList2;
            mAdapter.notifyDataSetChanged();
            mPager.setCurrentItem(getIntent().getIntExtra(EXTRA_INDEX, 0));
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
Bosen
  • 941
  • 2
  • 12
  • 26
  • What makes you think that the code you posted is relevant to your problem? Shouldn't you show us how you manipulate your fragments list? – kalabalik Aug 09 '16 at 14:45
  • How do you call requestPermission ? As per this post http://stackoverflow.com/questions/33169455/onrequestpermissionsresult-not-being-called-in-dialog-fragment you can see that if you call the support library one it will point back to your fragment directly.. is that a solution for you? – Cata Aug 09 '16 at 14:46
  • @KalaBalik What do you mean by how i manipulate fragments list? Are you referring to the getItem() and the getCount() in my FragmentStatePagerAdapter part? – Bosen Aug 09 '16 at 14:53
  • @Cata I am using the Fragment from the support library, not the dialog fragment. Does it work on Support Library one as well? I am also not using nested fragment like the post mentioned. – Bosen Aug 09 '16 at 14:57
  • @CodingNoobie Since your `ViewPager` does "weird" things, I was just wondering if your adapter needs updating whenever you modify a fragment in your list. If you are using a `FragmentStatePagerAdapter` it would be `adapter.notifyDataSetChanged()`. – kalabalik Aug 09 '16 at 15:20
  • @KalaBalik I have used the notifyDataSetChanged() in my loadData() method, which basically get the latest data and put in a Arraylist and then change the data for the adapter. That is when i call the notifyDataSetChange(). I have included the code in the post for reference. – Bosen Aug 10 '16 at 05:46

1 Answers1

0

I have found the bug. Basically, I made the mistake of putting the loadData() method in the onResume() method, hence setting the Viewpager page back to the one it got from that Extra.

Bosen
  • 941
  • 2
  • 12
  • 26