bubu uwu
bubu uwu

Reputation: 483

initialize fragment in android

I only know how to add fragment and remove. also show and hide.

I just want to initialize fragment.

onCreate {
    getFragmentManager().beginTransaction().add(R.id.top_container, new AuthProgressDialog(), AuthProgressDialog.class.getSimpleName()).commit();
    getFragmentManager().beginTransaction().hide( getFragmentManager().findFragmentByTag(AuthProgressDialog.class.getSimpleName()) ).commit();
}

This is bad code, isn't it?

If you have any idea, please let me know. Thanks.

Upvotes: 0

Views: 413

Answers (2)

Ajay Pandya
Ajay Pandya

Reputation: 2457

This is sample method which you can define inside your FragmentActivity and call from any of the fragment. in which if you want to allowing adding to backstack than just remove comment line from transaction.addToBackStack("back"); and check popup backstack as per your requirement inside FragmentActivity.

  public void displayView(int position) {

    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new FormFragment();
        break;
    case 1:
        fragment = new HomeFragment();
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager
                .beginTransaction();
        if (position != 0)
            // transaction.addToBackStack("back");
            transaction.replace(R.id.main_ll_container, fragment).commit();
    } else {
        Log.e("MainActivity", "Error in creating fragment");
    }
}

Upvotes: 0

user5713256
user5713256

Reputation:

Use this code :

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Fragment fraggy = new DummyFragment();
fragmentTransaction.add(R.id.fragment_container, fraggy);
fragmentTransaction.commit();

Upvotes: 0

Related Questions