【问题标题】:How to call a fragment from a class如何从类中调用片段
【发布时间】:2019-12-30 20:01:39
【问题描述】:

我只想从这个类中调用片段。 实际上,我的应用程序中有很多片段,必须一次又一次地调用它们。 所以我想创建一个类和一个函数来加载一个片段,所以每当我需要调用一个片段时,我都可以使用这个类的函数。 但我无法在此处获取 getSupportFragmentManager()。 我尝试通过将类扩展为片段,但随后它产生空异常。 也可以通过 Appcompactactivity 扩展并使用 getSupportFragmentManager(); 但也会通过说活动被破坏来给出错误。 那么有人有解决方案从一个简单的类中调用一个片段吗?

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();






    }
}

【问题讨论】:

    标签: android android-fragments fragment android-fragmentactivity fragmentmanager


    【解决方案1】:

    我相信你会在某个时候从一些 activityfragment 调用这个类,如果是这样,那么使用修改后的构造函数它将打开片段。否则,如果没有来自活动或片段的 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();
    
            }
        }
    

    【讨论】:

      【解决方案2】:

      您的类必须扩展 AppCompatActivity 类才能调用此函数,并且当您扩展该类时,您还必须覆盖活动的 onCreate() 方法,并且您拥有还要在 onCreate() 方法中为活动设置布局

      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();
      
      
      
      
      
      
      }
      }
      

      【讨论】:

      • 但我不需要那个......我只想使用这个类的功能而不使其活动。
      • 然后你可以在你的类中创建一个 AppCompatActivity 类的实例,并在你的类的构造函数中初始化它,然后你可以从 appCompatActivity 的对象调用函数
      • 你能用代码例子解释一下吗……因为我没听懂你
      • 好的,我明白了...但是在片段中制作对象时要为 appcompatactivity 传递对象参数是什么? CompletedandPendingScreensLoader screenLoader = new CompletedandPendingScreensLoader(---这里要传递什么 ---);
      • 如果您在活动中制作此类的对象传递“this”,如果您在片段中制作this的对象传递“getActivty()”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      相关资源
      最近更新 更多