title: Prevent Android Fragment from Being Recreated on Device Rotate
body: <h2>The Problem:</h2>
  
  <p>When I rotate my device, the information contained within my fragment in my app is reloaded. Since this app pulls information from the internet, this means, a lack of data for some time while data is being recollected.</p>
  
  <h2>What I have Tried:</h2>
  
  <p>I have read many articles and S/O questions that give a few general pointers and have tried their solutions, from <code>android:configChanges="orientation|screenSize"</code> in the manifest to adding <code>setRetainInstance(true);</code> in both my <code>MainActivity</code> and <code>FragmentActivity</code>. Neither of these have worked to keep my fragment from reloading. I used the <code>android:configChanges</code> in another activity in the app (A detail activity, which activates when a user selects a news story) and it works properly there, preventing the activity from reloading.</p>
  
  <h2>Structure of My App:</h2>
  
  <p>This is a simple app that pulls data from the "Hacker News" API and shows it to the user. There is a NavigationDrawer for the user to select different news types. Each item in the NavigationDrawer is a different Fragment that loads in the <code>MainActivity.class</code>. The Fragment in question here is <code>HomeFragment.class</code> which loads the main news stories. </p>
  
  <h2>Why I'm asking here:</h2>
  
  <p>I can't seem to find anything that works with the structure of my app as it currently stands, so I would like some help in trying to find a solution for this seemingly trivial (but confusing) problem.</p>
  
  <h2>Code:</h2>
  
  <h3>MainActivity.java</h3>
  
  <pre><code>package com.material.tdapps.hackernews.activity;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  
  import com.material.tdapps.hackernews.R;
  
  
  public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Toolbar appToolbar = (Toolbar) findViewById(R.id.toolbar);
  
          setSupportActionBar(appToolbar);
  
          //noinspection ConstantConditions
          getSupportActionBar().setDisplayShowHomeEnabled(true);
  
          FragmentDrawer drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
          drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), appToolbar);
          drawerFragment.setDrawerListener(this);
  
          displayView(0);
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
  
          return id == R.id.action_settings || super.onOptionsItemSelected(item);
  
      }
  
      @Override
      public void onDrawerItemSelected(View view, int position) {
          displayView(position);
      }
  
      private void displayView(int position){
          Fragment fragment = null;
          String title = getString(R.string.app_name);
          switch (position) {
              case 0:
                  fragment = new HomeFragment();
                  title = getString(R.string.title_news);
                  break;
              case 1:
                  fragment = new ShowFragment();
                  title = getString(R.string.title_show);
                  break;
              case 2:
                  fragment = new AskFragment();
                  title = getString(R.string.title_ask);
                  break;
              case 3:
                  fragment = new JobsFragment();
                  title = getString(R.string.title_job);
                  break;
              default:
                  break;
  
          }
  
          if (fragment != null){
              FragmentManager fragmentManager = getSupportFragmentManager();
              FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
              fragmentTransaction.replace(R.id.container_body, fragment);
              fragmentTransaction.commit();
  
              getSupportActionBar().setTitle(title);
  
          }
      }
  
  
  }
  </code></pre>
  
  <h3>HomeFragment.java</h3>
  
  <pre><code>package com.material.tdapps.hackernews.activity;
  
  import android.app.Activity;
  
  import android.content.Context;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.ImageButton;
  import android.widget.ListView;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  
  import com.material.tdapps.hackernews.JSON.JSONNewsParser;
  import com.material.tdapps.hackernews.R;
  import com.material.tdapps.hackernews.model.StoryFeed;
  import com.material.tdapps.hackernews.model.StoryItem;
  
  import java.net.MalformedURLException;
  
  
  public class HomeFragment extends Fragment {
  
      StoryFeed storyFeed;
      Context context;
      ListView listView;
      NewsListAdapter newsListAdapter;
  
      public HomeFragment() {
  
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
  
      }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_home, container, false);
          setRetainInstance(true);
          context = this.getActivity();
          storyFeed = new StoryFeed();
          newsListAdapter = new NewsListAdapter(this);
          listView = (ListView) rootView.findViewById(R.id.newsListView);
          if (savedInstanceState != null &amp;&amp; (savedInstanceState.getSerializable("previousFeed") != null)) {
              Log.e("NOTIFY &gt;&gt; ", "GOING TO BUNDLE");
              storyFeed = (StoryFeed)savedInstanceState.getSerializable("previousFeed");
              listView.setAdapter(newsListAdapter);
              newsListAdapter.notifyDataSetChanged();
          } else {
              Log.e("NOTIFY &gt;&gt; ", "GOING TO ASYNCTASK");
              new AsyncLoadNewsFeed().execute();
              listView.setAdapter(newsListAdapter);
          }
  
          setRetainInstance(true);
  
          return rootView;
  
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      }
  
      @Override
      public void onDetach() {
          super.onDetach();
      }
  
      private class AsyncLoadNewsFeed extends AsyncTask&lt;Void, Void, Void&gt; {
  
          @Override
          protected Void doInBackground(Void... params) {
              JSONNewsParser newsParser = new JSONNewsParser();
              storyFeed = newsParser.parseJSON("https://hacker-news.firebaseio.com/v0/topstories.json",0,30);
              return null;
          }
  
          @Override
          protected void onPostExecute(Void aVoid) {
              super.onPostExecute(aVoid);
              if (storyFeed == null || storyFeed.getStoryCount() == 0){
                  StoryItem nullStoryItem = new StoryItem();
                  nullStoryItem.setTitle("ERROR: Null error!");
                  nullStoryItem.setNumberComments(0);
                  nullStoryItem.setScore(0);
                  nullStoryItem.setBodyText("NULL");
                  nullStoryItem.setTime(0);
                  nullStoryItem.setAuthor("NULL");
                  nullStoryItem.setUrl("http://www.google.com");
                  storyFeed.addStory(nullStoryItem);
              }
              newsListAdapter.notifyDataSetChanged();
          }
      }
  
      class NewsListAdapter extends BaseAdapter {
  
          private LayoutInflater layoutInflater;
  
          public NewsListAdapter(HomeFragment homeFragment){
              layoutInflater = (LayoutInflater) homeFragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          }
  
          @Override
          public int getCount() {
              return storyFeed.getStoryCount();
          }
  
          @Override
          public Object getItem(int position) {
              return position;
          }
  
          @Override
          public long getItemId(int position) {
              return position;
          }
  
          class listViewHolder {
  
              TextView titleTxtV;
              TextView timeTxtV;
              TextView scoreTxtV;
              TextView authorTxtV;
              TextView domTxtV;
              Button commentB;
              RelativeLayout detailsLayout;
  
              listViewHolder(View v) {
                  titleTxtV = (TextView) v.findViewById(R.id.titleText);
                  timeTxtV = (TextView) v.findViewById(R.id.timeText);
                  scoreTxtV = (TextView) v.findViewById(R.id.pointsText);
                  authorTxtV = (TextView) v.findViewById(R.id.authorText);
                  domTxtV = (TextView) v.findViewById(R.id.domainText);
                  commentB = (Button) v.findViewById(R.id.commentButton);
                  detailsLayout = (RelativeLayout) v.findViewById(R.id.newsDetailRelLayout);
              }
  
          }
  
          @Override
          public View getView(final int position, View convertView, ViewGroup parent) {
  
              View listItem = convertView;
              listViewHolder holder;
  
              if (listItem == null) {
                  listItem = layoutInflater.inflate(R.layout.news_item_layout, parent, false );
                  holder = new listViewHolder(listItem);
                  listItem.setTag(holder);
              } else {
                  holder = (listViewHolder) listItem.getTag();
              }
  
  
  
              holder.titleTxtV.setText(storyFeed.getStory(position).getTitle());
              holder.timeTxtV.setText("Posted " + storyFeed.getStory(position).getTime());
              holder.authorTxtV.setText(" By " + storyFeed.getStory(position).getAuthor());
              holder.scoreTxtV.setText(storyFeed.getStory(position).getScore() + " Points ");
              holder.commentB.setText(Integer.toString(storyFeed.getStory(position).getNumberComments()));
              try {
                  holder.domTxtV.setText("(" + storyFeed.getStory(position).getURLDomain() + ")");
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              }
  
              holder.commentB.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent showComments = new Intent(getActivity(), CommentActivity.class);
                      getActivity().startActivity(showComments);
                  }
              });
  
              holder.detailsLayout.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Intent showDetails = new Intent(getActivity(), DetailWebActivity.class);
                      Bundle detailsBundle = new Bundle();
                      //detailsBundle.putSerializable("newsFeed", storyFeed);
                      showDetails.putExtra("news",storyFeed);
                      showDetails.putExtras(detailsBundle);
                      showDetails.putExtra("position", position);
                      getActivity().startActivity(showDetails);
                  }
              });
  
  
  
              return listItem;
  
          }
      }
  
  
  
  }
  </code></pre>
  
  <h3>AndroidManifest.xml</h3>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.material.tdapps.hackernews" &gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/HackerNewsTheme" &gt;
          &lt;activity
              android:name=".activity.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".activity.CommentActivity"
              android:label="@string/title_activity_comment"
              android:parentActivityName=".activity.MainActivity"
              android:configChanges="orientation|screenSize"&gt;
  
              &lt;meta-data
                  android:name="android.support.PARENT_ACTIVITY"
                  android:value="com.material.tdapps.hackernews.activity.MainActivity" /&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".activity.DetailWebActivity"
              android:label="@string/title_activity_detail_web"
              android:parentActivityName=".activity.MainActivity"
              android:configChanges="orientation|screenSize"&gt;
              &lt;meta-data
                  android:name="android.support.PARENT_ACTIVITY"
                  android:value="com.material.tdapps.hackernews.activity.MainActivity" /&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>