如何从 class 调用片段

How to call a fragment from a class

我只想从这个 class 中调用片段。 实际上我的应用程序中有很多片段,不得不一次又一次地调用它们。 所以我想制作一个 class 和一个用于加载片段的函数,这样每当我需要调用一个片段时,我都可以使用这个 class 的函数。 但是我无法在此处获取 getSupportFragmentManager()。 我尝试将 class 扩展到片段,但随后它会产生 null 异常。 也通过使用 Appcompactactivity 扩展并使用 getSupportFragmentManager(); 但也会通过说 activity 被破坏来给出错误。 所以任何人都有从简单的 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();






    }
}

您的 class 必须扩展 AppCompatActivity class 才能调用此函数,并且当您扩展 class 时,您还必须覆盖onCreate() 方法 activity 并且您还必须在 onCreate() 方法

中为 activity 设置布局
public class CompletedandPendingScreensLoader {

 private AppCompatActivity myActivty;

 public CompletedandPendingScreensLoader(AppCompatAcitivity myActivty)
 {
     this.myActivity = myActivity
   }


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 = 
    myActivity.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 = 
    myAcitvity.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 = 
     myActivity.getFragmentManager.beginTransaction();
    transaction.replace(R.id.frame, new LessonTwo()).addToBackStack(null); // replace a Fragment with Frame Layout
    transaction.commit();






}
}

我相信你会在某个时候从一些 activity 片段 中调用这个 class 如果是的话然后使用修改后的构造函数,它将打开片段。否则如果没有来自 activity 或 fragment.

FragmentManagerreference 就无法做到这一点。
  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();

        }
    }