title: Android using shared preferences to check on first run
body: <p>Hi I'm trying to detect whether my app has been opened for the first time. If it has, I need to run an activity and once it's opened for the second time it should not show it again.</p>
  
  <p>This is my code:</p>
  
  <p>fragment:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class);
      //startActivity(intent);
  
      SharedPreferences settings = this.getActivity().getSharedPreferences(PREFS_NAME, 0); // Get preferences file (0 = no option flags set)
      boolean firstRun = settings.getBoolean("firstRun", true); // Is it first run? If not specified, use "true"
  
      if(firstRun) {
          Log.w("onCreate: ","first time" );
          Intent intent = new Intent(getActivity(), TutorialFeaturedActivity.class);
          startActivity(intent);
  
          SharedPreferences.Editor editor = settings.edit(); // Open the editor for our settings
          editor.putBoolean("firstRun", false); // It is no longer the first run
          editor.apply(); // Save all changed settings
      } else {
          Log.w("onCreate: ","second time");
          Intent intent = new Intent(getActivity(), MainActivity.class);
          startActivity(intent);
      }
  
      getSpecials();
  }
  </code></pre>
  
  <p>But all it does is start the activity and when I start it again it freezes in a white screen but checking the logs it shows like the else statement is constantly running over and over. I'm fairly new to Android so some help or advice would be greatly appreciated</p>