'Android onRequestPermissionsResult utility of super
When I'm using onRequestPermissionsResultiis it is usefull to keep the super ?
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
If we look at the super method
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
mFragments.noteStateNotSaved();
int index = (requestCode >> 16) & 0xffff;
if (index != 0) {
index--;
String who = mPendingFragmentActivityResults.get(index);
mPendingFragmentActivityResults.remove(index);
if (who == null) {
Log.w(TAG, "Activity result delivered for unknown Fragment.");
return;
}
Fragment frag = mFragments.findFragmentByWho(who);
if (frag == null) {
Log.w(TAG, "Activity result no fragment exists for who: " + who);
} else {
frag.onRequestPermissionsResult(requestCode & 0xffff, permissions, grantResults);
}
}
}
I don't really understand the the goal of the super, maybe juste for the log ?
Can I delete safely the super method everywhere ?
Solution 1:[1]
When you are dealing with super
methods, check what it is doing by looking its source code. (Ctrl + Click).
In your case, the method onRequestPermissionsResult
of the class FragmentActivity
(parent class of AppCompatActivity
) has the code to forward the results to its fragments. So if you need the results in fragments, it is necessary, otherwise you can remove the super call. Also if you are directly using Activity
class for your activity, you can remove it.
In the case of Fragment
the method onRequestPermissionsResult
has no body. So you can remove the super call.
Solution 2:[2]
Not calling .super()
is no longer an option. Don't know what version this changed, but Android Studio now insists on calling .super()
for any subclass of AppCompatActivity.
Oddly the code still compiles and runs even with this error! I'm guessing this is to keep from breaking old code. Look for compiler errors in the near future.
Lesson: call .super()
. Looks like google really wants you to.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nabin Bhandari |
Solution 2 | SMBiggs |