gt: 		if(fragment == null) return;
context: package com.wellsandwhistles.android.redditsp.activities;
  
  /** This file was either copied or modified from https://github.com/QuantumBadger/RedReader
   * under the Free Software Foundation General Public License version 3*/
  
  import android.content.Intent;
  import android.graphics.drawable.ColorDrawable;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  import android.support.annotation.Nullable;
  import android.support.v7.app.AppCompatActivity;
  import android.view.Menu;
  import android.view.View;
  import com.wellsandwhistles.android.redditsp.R;
  import com.wellsandwhistles.android.redditsp.account.RedditAccount;
  import com.wellsandwhistles.android.redditsp.account.RedditAccountChangeListener;
  import com.wellsandwhistles.android.redditsp.account.RedditAccountManager;
  import com.wellsandwhistles.android.redditsp.common.DialogUtils;
  import com.wellsandwhistles.android.redditsp.common.General;
  import com.wellsandwhistles.android.redditsp.common.LinkHandler;
  import com.wellsandwhistles.android.redditsp.common.PrefsUtility;
  import com.wellsandwhistles.android.redditsp.fragments.PostListingFragment;
  import com.wellsandwhistles.android.redditsp.listingcontrollers.PostListingController;
  import com.wellsandwhistles.android.redditsp.reddit.PostSort;
  import com.wellsandwhistles.android.redditsp.reddit.api.RedditSubredditSubscriptionManager;
  import com.wellsandwhistles.android.redditsp.reddit.prepared.RedditPreparedPost;
  import com.wellsandwhistles.android.redditsp.reddit.things.RedditSubreddit;
  import com.wellsandwhistles.android.redditsp.reddit.url.PostCommentListingURL;
  import com.wellsandwhistles.android.redditsp.reddit.url.PostListingURL;
  import com.wellsandwhistles.android.redditsp.reddit.url.RedditURLParser;
  import com.wellsandwhistles.android.redditsp.reddit.url.SearchPostListURL;
  
  import java.util.Locale;
  import java.util.UUID;
  
  public class PostListingActivity extends RefreshableActivity
  		implements RedditAccountChangeListener,
  		RedditPreparedPost.PostSelectionListener,
  		OptionsMenuUtility.OptionsMenuPostsListener,
  		SessionChangeListener,
  		RedditSubredditSubscriptionManager.SubredditSubscriptionStateChangeListener {
  
  	private static final String SAVEDSTATE_SESSION = "pla_session";
  	private static final String SAVEDSTATE_SORT = "pla_sort";
  	private static final String SAVEDSTATE_FRAGMENT = "pla_fragment";
  
  	private PostListingFragment fragment;
  	private PostListingController controller;
  
  	public void onCreate(final Bundle savedInstanceState) {
  
  		PrefsUtility.applyTheme(this);
  
  		super.onCreate(savedInstanceState);
  
  		getWindow().setBackgroundDrawable(new ColorDrawable(obtainStyledAttributes(new int[] {R.attr.srListBackgroundCol}).getColor(0,0)));
  
  		RedditAccountManager.getInstance(this).addUpdateListener(this);
  
  		if(getIntent() != null) {
  
  			final Intent intent = getIntent();
  
  			final RedditURLParser.RedditURL url = RedditURLParser.parseProbablePostListing(intent.getData());
  
  			if(!(url instanceof PostListingURL)) {
  				throw new RuntimeException(String.format(Locale.US, "'%s' is not a post listing URL!", url.generateJsonUri()));
  			}
  
  			controller = new PostListingController((PostListingURL)url, this);
  
  			Bundle fragmentSavedInstanceState = null;
  
  			if(savedInstanceState != null) {
  
  				if(savedInstanceState.containsKey(SAVEDSTATE_SESSION)) {
  					controller.setSession(UUID.fromString(savedInstanceState.getString(SAVEDSTATE_SESSION)));
  				}
  
  				if(savedInstanceState.containsKey(SAVEDSTATE_SORT)) {
  					controller.setSort(PostSort.valueOf(
  							savedInstanceState.getString(SAVEDSTATE_SORT)));
  				}
  
  				if(savedInstanceState.containsKey(SAVEDSTATE_FRAGMENT)) {
  					fragmentSavedInstanceState = savedInstanceState.getBundle(SAVEDSTATE_FRAGMENT);
  				}
  			}
  
  			setTitle(url.humanReadableName(this, false));
  
  			setBaseActivityContentView(R.layout.main_single);
  			doRefresh(RefreshableFragment.POSTS, false, fragmentSavedInstanceState);
  
  		} else {
  			throw new RuntimeException("Nothing to show!");
  		}
  
  		addSubscriptionListener();
  	}
  
  	@Override
  	protected void onSaveInstanceState(final Bundle outState) {
  		super.onSaveInstanceState(outState);
  
  		final UUID session = controller.getSession();
  		if(session != null) {
  			outState.putString(SAVEDSTATE_SESSION, session.toString());
  		}
  
  		final PostSort sort = controller.getSort();
  		if(sort != null) {
  			outState.putString(SAVEDSTATE_SORT, sort.name());
  		}
  
  		if(fragment != null) {
  			outState.putBundle(SAVEDSTATE_FRAGMENT, fragment.onSaveInstanceState());
  		}
  	}
  
  	@Override
  	public boolean onCreateOptionsMenu(final Menu menu) {
  
  		final RedditAccount user = RedditAccountManager.getInstance(this).getDefaultAccount();
  		final RedditSubredditSubscriptionManager.SubredditSubscriptionState subredditSubscriptionState;
  		final RedditSubredditSubscriptionManager subredditSubscriptionManager
  				= RedditSubredditSubscriptionManager.getSingleton(this, user);
  
  		if(!user.isAnonymous()
  				&& controller.isSubreddit()
  				&& subredditSubscriptionManager.areSubscriptionsReady()
  				&& fragment != null
  				&& fragment.getSubreddit() != null) {
  
  			subredditSubscriptionState = subredditSubscriptionManager.getSubscriptionState(controller.subredditCanonicalName());
  
  		} else {
  			subredditSubscriptionState = null;
  		}
  
  		final String subredditDescription = fragment != null && fragment.getSubreddit() != null
  				? fragment.getSubreddit().description_html : null;
  
  		Boolean subredditPinState = null;
  		Boolean subredditBlockedState = null;
  
  		if(controller.isSubreddit()
  				&& fragment != null
  				&& fragment.getSubreddit() != null) {
  
  			try {
  				subredditPinState = PrefsUtility.pref_pinned_subreddits_check(
  						this,
  						PreferenceManager.getDefaultSharedPreferences(this),
  						fragment.getSubreddit().getCanonicalName());
  
  				subredditBlockedState = PrefsUtility.pref_blocked_subreddits_check(
  						this,
  						PreferenceManager.getDefaultSharedPreferences(this),
  						fragment.getSubreddit().getCanonicalName());
  
  			} catch(RedditSubreddit.InvalidSubredditNameException e) {
  				subredditPinState = null;
  				subredditBlockedState = null;
  			}
  		}
  
  		OptionsMenuUtility.prepare(
  				this,
  				menu,
  				false,
  				true,
  				false,
  				controller.isSearchResults(),
  				controller.isUserPostListing(),
  				false, controller.isSortable(),
  				true,
  				subredditSubscriptionState,
  				subredditDescription != null && subredditDescription.length() > 0,
  				subredditPinState,
  				subredditBlockedState);
  
  		return true;
  	}
  
  	private void addSubscriptionListener() {
  		RedditSubredditSubscriptionManager
  				.getSingleton(this, RedditAccountManager.getInstance(this).getDefaultAccount())
  				.addListener(this);
  	}
  
  	public void onRedditAccountChanged() {
  		addSubscriptionListener();
  		postInvalidateOptionsMenu();
  		requestRefresh(RefreshableFragment.ALL, false);
  	}
  
  	@Override
  	protected void doRefresh(final RefreshableFragment which, final boolean force, final Bundle savedInstanceState) {
  		if(fragment != null) fragment.cancel();
  		fragment = controller.get(this, force, savedInstanceState);
  
  		final View view = fragment.getView();
  		setBaseActivityContentView(view);
  		General.setLayoutMatchParent(view);
  	}
  
  	public void onPostSelected(final RedditPreparedPost post) {
  		LinkHandler.onLinkClicked(this, post.src.getUrl(), false, post.src.getSrc());
  	}
  
  	public void onPostCommentsSelected(final RedditPreparedPost post) {
  		LinkHandler.onLinkClicked(this, PostCommentListingURL.forPostId(post.src.getIdAlone()).toString(), false);
  	}
  
  	public void onRefreshPosts() {
  		controller.setSession(null);
  		requestRefresh(RefreshableFragment.POSTS, true);
  	}
  
  	public void onSubmitPost() {
  		final Intent intent = new Intent(this, PostSubmitActivity.class);
  		if(controller.isSubreddit()) {
  			intent.putExtra("subreddit", controller.subredditCanonicalName());
  		}
  		startActivity(intent);
  	}
  
  	public void onSortSelected(final PostSort order) {
  		controller.setSort(order);
  		requestRefresh(RefreshableFragment.POSTS, false);
  	}
  
  	@Override
  	public void onSearchPosts() {
  		onSearchPosts(controller, this);
  	}
  
  	public static void onSearchPosts(final PostListingController controller, final AppCompatActivity activity) {
  
  		DialogUtils.showSearchDialog(activity, new DialogUtils.OnSearchListener() {
  			@Override
  			public void onSearch(@Nullable String query) {
  				if (query == null)	return;
  
  				final SearchPostListURL url;
  
  				if(controller != null && (controller.isSubreddit() || controller.isSubredditSearchResults())) {
  					url = SearchPostListURL.build(controller.subredditCanonicalName(), query);
  				} else {
  					url = SearchPostListURL.build(null, query);
  				}
  
  				final Intent intent = new Intent(activity, PostListingActivity.class);
  				intent.setData(url.generateJsonUri());
  				activity.startActivity(intent);
  			}
  		});
  	}
  
  	@Override
  	public void onSubscribe() {
  		fragment.onSubscribe();
  	}
  
  	@Override
  	public void onUnsubscribe() {
  		fragment.onUnsubscribe();
  	}
  
  	@Override
  	public void onSidebar() {
  		final Intent intent = new Intent(this, HtmlViewActivity.class);
  		intent.putExtra("html", fragment.getSubreddit().getSidebarHtml(PrefsUtility.isNightMode(this)));
  		intent.putExtra("title", String.format(Locale.US, "%s: %s",
  				getString(R.string.sidebar_activity_title),
  				fragment.getSubreddit().url));
  		startActivityForResult(intent, 1);
  	}
  
  	@Override
  	public void onPin() {
  
  		if(fragment == null) return;
  
  		try {
  			PrefsUtility.pref_pinned_subreddits_add(
  					this,
  					PreferenceManager.getDefaultSharedPreferences(this),
  					fragment.getSubreddit().getCanonicalName());
  
  		} catch(RedditSubreddit.InvalidSubredditNameException e) {
  			throw new RuntimeException(e);
  		}
  
  		invalidateOptionsMenu();
  	}
  
  	@Override
  	public void onUnpin() {
  
  		if(fragment == null) return;
  
  		try {
  			PrefsUtility.pref_pinned_subreddits_remove(
  					this,
  					PreferenceManager.getDefaultSharedPreferences(this),
  					fragment.getSubreddit().getCanonicalName());
  
  		} catch(RedditSubreddit.InvalidSubredditNameException e) {
  			throw new RuntimeException(e);
  		}
  
  		invalidateOptionsMenu();
  	}
  
  	@Override
  	public void onBlock() {
  