mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
Null checks for activity to prevent unwanted crashes (#1730)
* Null checks for activity to prevent unwanted crashes * With logs before return statements
This commit is contained in:
parent
e13e2c2f40
commit
4ea7229876
1 changed files with 32 additions and 3 deletions
|
|
@ -94,6 +94,12 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
view.postDelayed(() -> {
|
||||
pager.setAdapter(adapter);
|
||||
pager.setCurrentItem(pageNumber, false);
|
||||
|
||||
if(getActivity() == null) {
|
||||
Timber.d("Returning as activity is destroyed!");
|
||||
return;
|
||||
}
|
||||
|
||||
getActivity().supportInvalidateOptionsMenu();
|
||||
adapter.notifyDataSetChanged();
|
||||
}, 100);
|
||||
|
|
@ -123,6 +129,10 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if(getActivity() == null) {
|
||||
Timber.d("Returning as activity is destroyed!");
|
||||
return true;
|
||||
}
|
||||
MediaDetailProvider provider = (MediaDetailProvider) getActivity();
|
||||
Media m = provider.getMediaAtPosition(pager.getCurrentItem());
|
||||
switch (item.getItemId()) {
|
||||
|
|
@ -189,10 +199,13 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
* @param m Media file to download
|
||||
*/
|
||||
private void downloadMedia(Media m) {
|
||||
String imageUrl = m.getImageUrl(),
|
||||
fileName = m.getFilename();
|
||||
String imageUrl = m.getImageUrl(), fileName = m.getFilename();
|
||||
|
||||
if (imageUrl == null || fileName == null) {
|
||||
if (imageUrl == null
|
||||
|| fileName == null
|
||||
|| getContext() == null
|
||||
|| getActivity() == null) {
|
||||
Timber.d("Skipping download media as either imageUrl %s or filename %s activity is null", imageUrl, fileName);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -234,6 +247,10 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
inflater.inflate(R.menu.fragment_image_detail, menu);
|
||||
if (pager != null) {
|
||||
MediaDetailProvider provider = (MediaDetailProvider) getActivity();
|
||||
if(provider == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Media m = provider.getMediaAtPosition(pager.getCurrentItem());
|
||||
if (m != null) {
|
||||
// Enable default set of actions, then re-enable different set of actions only if it is a failed contrib
|
||||
|
|
@ -285,6 +302,10 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
|
||||
@Override
|
||||
public void onPageScrolled(int i, float v, int i2) {
|
||||
if(getActivity() == null) {
|
||||
Timber.d("Returning as activity is destroyed!");
|
||||
return;
|
||||
}
|
||||
if (i+1 >= adapter.getCount()){
|
||||
try{
|
||||
((CategoryImagesActivity) getContext()).requestMoreImages();
|
||||
|
|
@ -336,6 +357,10 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
public Fragment getItem(int i) {
|
||||
if (i == 0) {
|
||||
// See bug https://code.google.com/p/android/issues/detail?id=27526
|
||||
if(getActivity() == null) {
|
||||
Timber.d("Skipping getItem. Returning as activity is destroyed!");
|
||||
return null;
|
||||
}
|
||||
pager.postDelayed(() -> getActivity().supportInvalidateOptionsMenu(), 5);
|
||||
}
|
||||
return MediaDetailFragment.forMedia(i, editable, isFeaturedImage);
|
||||
|
|
@ -343,6 +368,10 @@ public class MediaDetailPagerFragment extends CommonsDaggerSupportFragment imple
|
|||
|
||||
@Override
|
||||
public int getCount() {
|
||||
if(getActivity() == null) {
|
||||
Timber.d("Skipping getCount. Returning as activity is destroyed!");
|
||||
return 0;
|
||||
}
|
||||
return ((MediaDetailProvider) getActivity()).getTotalMediaCount();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue