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) {
}
});
}