I'm trying to write a method that would replace the code that I use to swap fragments in order to keep copying and posting to a minimum (and to stay D.R.Y.)
The ProblemI get an error when I attempt to use the class that I passed in as an argument to create a new instance of that class.
The error occurs in this line of code, to the left of the operator (equal sign):
newFragmentClass new_fragment = newFragmentClass.newInstance();
The error it gives me is: "newFragmentClass cannot be resolved to a type".
Full Code private void changeFragment(Class<?> newFragmentClass)
{
// Detect the current fragment
Fragment current_fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
// Create a new instance of the given class, edit later to gracefully handle errors
newFragmentClass new_fragment = newFragmentClass.newInstance();
// Switch to the new fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.detach(current_fragment);
transaction.replace(R.id.fragment_container, new_fragment);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
// Change the tab background to indicate that the tab is active, reset backgrounds for any other tabs
findViewById(R.id.page_one_tab).setBackgroundResource(R.drawable.menu_button_background_active);
findViewById(R.id.page_two_tab).setBackgroundResource(R.drawable.menu_button_background);
findViewById(R.id.page_three_tab).setBackgroundResource(R.drawable.menu_button_background);
}
// Page One Tab Button Functionality
public void pageOneTab (View v)
{
// Change the fragment to SelectPlayersFragment
changeFragment(Page_One_Fragment.class);
}
Attempted Solutions
I've been searching StackOverflow and the internet at large for quite a while and have not been able to find a solution. A few topics like this one seemed as if they would resolve the problem, but then I ran into an error on the transaction.replace line that I could not find a fix for: "The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Object)".