Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Fix forum crashes when view or activity is null
Browse files Browse the repository at this point in the history
This commit fixes the case when a network task returns (whether it be a
success or a failure) if the fragment has been destroyed due to pager
having moved to another page or the activity being finished, a crash
occurs cuz it can't find a non-null activity context or fragment's view.
  • Loading branch information
miankhalid authored and christopher lee committed Jul 11, 2016
1 parent bf833d4 commit 7e6efba
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.edx.mobile.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -212,18 +213,22 @@ private void fetchDiscussionTopic() {
@Override
protected void onSuccess(CourseTopics courseTopics) throws Exception {
discussionTopic = courseTopics.getCoursewareTopics().get(0).getChildren().get(0);
if (!getArguments().getBoolean(ARG_DISCUSSION_HAS_TOPIC_NAME)) {
Activity activity = getActivity();
if (activity != null &&
!getArguments().getBoolean(ARG_DISCUSSION_HAS_TOPIC_NAME)) {
// We only need to set the title here when coming from a deep link
getActivity().setTitle(discussionTopic.getName());
activity.setTitle(discussionTopic.getName());
}

if (populatePostListRunnable != null) {
setProgressDialog(null);
populatePostListRunnable.run();
}
if (getView() != null) {
if (populatePostListRunnable != null) {
setProgressDialog(null);
populatePostListRunnable.run();
}

// Now that we have the topic date, we can allow the user to add new posts.
createNewPostLayout.setEnabled(true);
// Now that we have the topic date, we can allow the user to add new posts.
createNewPostLayout.setEnabled(true);
}
}
};
getTopicsTask.setProgressDialog(loadingIndicator);
Expand Down Expand Up @@ -315,6 +320,7 @@ private void populatePostList(@NonNull final InfiniteScrollUtils.PageLoadCallbac
discussionTopic, postsFilter, postsSort, nextPage) {
@Override
public void onSuccess(Page<DiscussionThread> threadsPage) {
if (getView() == null) return;
++nextPage;
callback.onPageLoaded(threadsPage);

Expand All @@ -333,6 +339,7 @@ public void onSuccess(Page<DiscussionThread> threadsPage) {

@Override
protected void onException(Exception ex) {
if (getView() == null) return;
// Don't display any error message if we're doing a silent
// refresh, as that would be confusing to the user.
if (!callback.isRefreshingSilently()) {
Expand Down

0 comments on commit 7e6efba

Please sign in to comment.