So, I am writing a navigation part of my app and have dynamically created fragments inside an activity.
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.
I thought that I would be able to do this by using
getSupportFragmentManager().findFragmentById(R.id.activity_public_internet)
but that doesn't seem to work in the switch statement.
So I then tried
getFragmentManager().findFragmentById(R.id.public_internet_intro_fragment).getId()
and that doesn't work either.
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.
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;
}
}
When checking the error log, a Null Pointer Exception is raised after attempting to invoke int android.app.Fragment.getId()
EDIT: Added PublicInternetActivity.java
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;
}
}
}