title: Android Getting Fragment ID for use in Switch statement
body: <p>So, I am writing a navigation part of my app and have dynamically created fragments inside an activity. </p>
  
  <p>I want, when a user hits either the next or previous arrow, for the app to find out what fragment is in the activity and replace it with either the next or previous fragment. In order to do that, I created a switch statement to check on what fragment is being displayed.</p>
  
  <p>I thought that I would be able to do this by using </p>
  
  <pre><code>getSupportFragmentManager().findFragmentById(R.id.activity_public_internet)
  </code></pre>
  
  <p>but that doesn't seem to work in the switch statement.</p>
  
  <p>So I then tried </p>
  
  <pre><code>getFragmentManager().findFragmentById(R.id.public_internet_intro_fragment).getId()
  </code></pre>
  
  <p>and that doesn't work either.</p>
  
  <p>Here is the full code. Any help will be fully appreciated. And feel free to tell me if there is a better way to do it.</p>
  
  <pre><code>public void goPrev(View view) {
          switch(getSupportFragmentManager().findFragmentById(R.id.activity_public_internet).getId()) {
              case R.id.public_internet_intro_fragment:
                  Intent intent = new Intent(this, LearnActivity.class);
                  startActivity(intent);
                  break;
              case R.id.public_internet_topic_fragment:
                  Fragment introFragment = new PublicInternetIntroFragment();
                  FragmentTransaction exampleTransaction = getFragmentManager().beginTransaction();
                  exampleTransaction.replace(R.id.activity_public_internet, introFragment);
                  exampleTransaction.addToBackStack(null);
                  exampleTransaction.commit();
                  break;
              case R.id.public_internet_example_fragment:
                  Fragment topicFragment = new PublicInternetTopicFragment();
                  FragmentTransaction topicTransaction = getFragmentManager().beginTransaction();
                  topicTransaction.replace(R.id.activity_public_internet, topicFragment);
                  topicTransaction.addToBackStack(null);
                  topicTransaction.commit();
                  break;
          }
      }
  
      public void goNext(View view) {
          switch(getFragmentManager().findFragmentById(R.id.public_internet_intro_fragment).getId()) {
              case R.id.public_internet_intro_fragment:
                  Fragment topicFragment = new PublicInternetTopicFragment();
                  FragmentTransaction topicTransaction = getFragmentManager().beginTransaction();
                  topicTransaction.replace(R.id.activity_public_internet, topicFragment);
                  topicTransaction.addToBackStack(null);
                  topicTransaction.commit();
                  break;
              case R.id.public_internet_topic_fragment:
                  Fragment exampleFragment = new PublicInternetExampleFragment();
                  FragmentTransaction exampleTransaction = getFragmentManager().beginTransaction();
                  exampleTransaction.replace(R.id.activity_public_internet, exampleFragment);
                  exampleTransaction.addToBackStack(null);
                  exampleTransaction.commit();
                  break;
              case R.id.public_internet_example_fragment:
                  Intent intent = new Intent(this, LearnActivity.class);
                  startActivity(intent);
                  break;
          }
      }
  </code></pre>
  
  <p>When checking the error log, a Null Pointer Exception is raised after attempting to invoke <code>int android.app.Fragment.getId()</code></p>
  
  <p>EDIT: Added PublicInternetActivity.java</p>
  
  <pre><code>import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentTransaction;
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v4.app.NavUtils;
  import android.support.v7.app.ActionBarActivity;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.WindowManager;
  
  public class PublicInternetActivity extends ActionBarActivity{
      private static final String tag_public_internet_intro_fragment = "public_internet_intro_fragment";
      private static final String tag_public_internet_topic_fragment = "public_internet_topic_fragment";
      private static final String tag_public_internet_example_fragment = "public_internet_example_fragment";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
  
          setContentView(R.layout.activity_public_internet);
  
          introFrag();
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu items for use in the action bar
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu_learn, menu);
          return super.onCreateOptionsMenu(menu);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
  
          if (id == R.id.action_home) {
              goHome();
              return true;
          }
          else {
              return super.onOptionsItemSelected(item);
          }
      }
  
      public void introFrag() {
          Fragment introFragment = new PublicInternetIntroFragment();
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
          transaction.replace(R.id.activity_public_internet, introFragment);
          transaction.addToBackStack(null);
          transaction.commit();
      }
  
      public void goHome() {
          Intent homeIntent = NavUtils.getParentActivityIntent(this);
          NavUtils.navigateUpTo(this, homeIntent);
      }
  
      public void pushNewFragment( Fragment newFrag, String tag) {
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  
          transaction.replace(R.id.activity_public_internet, newFrag, tag);
          transaction.addToBackStack(tag);
          transaction.commit();
      }
  
      public String getActiveFragmentTag() {
          if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
             return null;
          }
          String tag = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1).getName();
          return tag;
      }
  
      public void goPrev(View view) {
          switch(getActiveFragmentTag()) {
              case tag_public_internet_intro_fragment:
                  Intent intent = new Intent(this, LearnActivity.class);
                  startActivity(intent);
                  break;
              case tag_public_internet_topic_fragment:
                  Fragment introFragment = new PublicInternetIntroFragment();
                  pushNewFragment(introFragment, tag_public_internet_intro_fragment);
                  break;
              case tag_public_internet_example_fragment:
                  Fragment topicFragment = new PublicInternetTopicFragment();
                  pushNewFragment(topicFragment, tag_public_internet_topic_fragment);
                  break;
          }
      }
  
      public void goNext(View view) {
          switch(getActiveFragmentTag()) {
              case tag_public_internet_intro_fragment:
                  Fragment topicFragment = new PublicInternetTopicFragment();
                  pushNewFragment(topicFragment, tag_public_internet_topic_fragment);
                  break;
              case tag_public_internet_topic_fragment:
                  Fragment exampleFragment = new PublicInternetExampleFragment();
                  pushNewFragment(exampleFragment, tag_public_internet_example_fragment);
                  break;
              case tag_public_internet_example_fragment:
                  Intent intent = new Intent(this, LearnActivity.class);
                  startActivity(intent);
                  break;
          }
      }
  }
  </code></pre>