如何在Android中从另一个类添加片段到活动

时间:2016-11-10 20:01:08

标签: android android-fragments android-activity

我试图通过调用活动的某些方法将片段添加到活动的容器(FrameLayout)。

所以这是我的活动类的简化版本:

public class TermsAndConditionsActivity extends AppCompatActivity {

private static TermsAndConditionsActivity           instance;
private static boolean                              loggedIn;
private TermsAndConditionsListener                  termsAndConditionsListener;

private TermsAndConditionsFragment                  termsAndConditionsFragment;
private ReadTermsAndConditionsFragment              readTermsAndConditionsFragment;
private Terms                                       terms;
private User                                        user;
protected FragmentManager                           fragmentManager;

public TermsAndConditionsActivity() {}

public static TermsAndConditionsActivity getInstance(Terms terms, User user, boolean loggedIn, TermsAndConditionsListener termsAndConditionsListener) {
    if (instance == null) {
        instance                            = new TermsAndConditionsActivity();
        instance.terms                      = terms;
        instance.user                       = user;
        instance.loggedIn                   = loggedIn;
        instance.termsAndConditionsListener = termsAndConditionsListener;
    }
    return instance;
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_main_activity_no_drawer);
    fragmentManager = getSupportFragmentManager();
    prepareReadTermsAndConditionsFragment();
    prepareTermsAndConditionsFragment();
}

private void prepareTermsAndConditionsFragment() {
    if (termsAndConditionsFragment == null) {
        if (loggedIn) {
            termsAndConditionsFragment = TermsAndConditionsOnlineFragment.newInstance(termsAndConditionsListener);
        } else {
            termsAndConditionsFragment = TermsAndConditionsAnonymousFragment.newInstance(termsAndConditionsListener);
        }
    }
}
private void prepareReadTermsAndConditionsFragment() {
    if (readTermsAndConditionsFragment == null) {
        readTermsAndConditionsFragment = ReadTermsAndConditionsFragment.newInstance(terms, user, termsAndConditionsListener);
    }
}

public void openTermsAndConditions() {
    initFragment(termsAndConditionsFragment);
}

public void openReadTermsAndConditions() {
    initFragment(readTermsAndConditionsFragment);
}

private void initFragment(TermsAndConditionsFragment fragment) {
    if (fragment != null) {
        FragmentTransaction ft = getFragmentTransaction();
        ft.add(getContainerId(), fragment, fragment.getTagString()).commit();
    }
}

protected FragmentTransaction getFragmentTransaction() {
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out);
    return ft;
}

public static void startActivity(Context context, int flags) {
    if (context != null) {
        Intent launchIntent                 = getIntent(context, flags);
        context.startActivity(launchIntent);
    }
}

public static Intent getIntent(Context context, int flags) {
    Intent launchIntent = new Intent(context, TermsAndConditionsActivity.class);
    launchIntent.addFlags(flags);
    return launchIntent;
}
}

活动类适用于创建活动的布局,这基本上是用于保存片段的Framelayout。

问题在于,如果我调用方法" openTermsAndConditions()"从活动课外,没有任何反应。但是,如果我在活动类中从onCreate调用它,则会正确添加它。我希望能够从活动类之外添加这些。

1 个答案:

答案 0 :(得分:0)

不要创建导致内存浪费的全局实例,只在所需的方法范围内创建它们,在你的情况下,你不需要全局的片段实例,因为你只使用它一次。

试试这个

public void addYourFragment()
 {
     Fragment fragment = getSupportFragmentManager.findFragmentByTag(TermsAndConditionsFragment.class.getSimpleName()); 
   if(fragment == null)
   {
     fragment = new TermsAndConditionsFragment();
   }
    if(fragment not added)
    {
    //call you initFragment,remove the first line in that and pass the Fragment parameter
     initFragment(Fragment fragment)
     {
      //do add or replace and make this common method for all your fragments to be added don't use too many methods for each diff fragments to be added
      }
 }