I just want to call fragment from this class. Actually i have many fragments in my app and have to call them again and again. So i thought to make a class and a function for loading a fragment so whenever i need to call a fragment i can use the function of this class. But iam unable to get getSupportFragmentManager() here. I tried by extending the class to fragment but then it produces null exception. also by extending with Appcompactactivity and uses getSupportFragmentManager(); but is also gives error by saying the activity destroyed. So anyone have solution to call a fragment from a simple class?
public class CompletedandPendingScreensLoader {
public void pendingscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("pending","pen");
frag.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void completedscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("completed","yes");
frag.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void simpleScreenLoader( int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
FragmentTransaction transaction = getFragmentManager.beginTransaction();
transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit();
}
}
I believe you will at some point call this class from some activity or fragment if so then use the modified constructor it will open the fragment. else you can't do it without having a reference for FragmentManager from activity or fragment.
public class CompletedandPendingScreensLoader {
private FragmentManager fragmentManager = null;
//when ever you start your class just start using this constructor
CompletedandPendingScreensLoader(FragmentManager fragmentManager){
this.fragmentManager = fragmentManager
}
public void pendingscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("pending","pen");
frag.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void completedscreenLoader(int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
Bundle bundle = new Bundle();
bundle.putString("completed","yes");
frag.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, frag).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit(); // commit the changes
}
}
public void simpleScreenLoader( int serialnumber){
Fragment frag = null;
switch (serialnumber){
case 1:
frag = new LessonOne();
break;
case 2:
frag = new LessonTwo();
break;
case 3:
frag = new LessonThree();
break;
case 4:
frag = new LessonFour();
break;
case 5:
frag = new LessonFive();
break;
}
if (frag != null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
transaction.commit();
}
}