Android replace the current fragment with another fragment

前端 未结 6 1259
迷失自我
迷失自我 2020-11-28 09:23

I just started with fragment design for HoneyComb. I created two fragments. When i click a button in the left side fragment, a new fragment is created in right side. Meanwh

相关标签:
6条回答
  • 2020-11-28 09:37

    Then provided your button is showing and the click event is being fired you can call the following in your click event:

    final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag"); 
    ft.commit(); 
    

    and if you want to go back to the DetailsFragment on clicking back ensure you add the above transaction to the back stack, i.e.

    ft.addToBackStack(null);
    

    Or am I missing something? Alternatively some people suggest that your activity gets the click event for the button and it has responsibility for replacing the fragments in your details pane.

    0 讨论(0)
  • 2020-11-28 09:40

    Use android.support.v4.app for FragmentManager & FragmentTransaction in your code, it has worked for me.

    DetailsFragment detailsFragment = new DetailsFragment();
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.details,detailsFragment);
    fragmentTransaction.commit();
    
    0 讨论(0)
  • 2020-11-28 09:45

    it's very simple how to replace with Fragment.

    DataFromDb changeActivity = new DataFromDb();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.changeFrg, changeActivity);
        transaction.commit();
    
    0 讨论(0)
  • 2020-11-28 09:48

    You can try below code. it’s very easy method for push new fragment from old fragment.

    private int mContainerId;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    private final static String TAG = "DashBoardActivity";
    
    public void replaceFragment(Fragment fragment, String TAG) {
    
        try {
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(mContainerId, fragment, tag);
            fragmentTransaction.addToBackStack(tag);
            fragmentTransaction.commitAllowingStateLoss();
    
        } catch (Exception e) {
            // TODO: handle exception
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 09:48

    If you have a handle to an existing fragment you can just replace it with the fragment's ID.

    Example in Kotlin:

    fun aTestFuction() {
       val existingFragment = MyExistingFragment() //Get it from somewhere, this is a dirty example
       val newFragment = MyNewFragment()
       replaceFragment(existingFragment, newFragment, "myTag")
    }
    
    fun replaceFragment(existing: Fragment, new: Fragment, tag: String? = null) {
        supportFragmentManager.beginTransaction().replace(existing.id, new, tag).commit()
    }
    
    0 讨论(0)
  • 2020-11-28 09:53

    Latest Stuff

    Okay. So this is a very old question and has great answers from that time. But a lot has changed since then.

    Now, in 2020, if you are working with Kotlin and want to change the fragment then you can do the following.

    1. Add Kotlin extension for Fragments to your project.

    In your app level build.gradle file add the following,

    dependencies {
        def fragment_version = "1.2.5"
    
        // Kotlin
        implementation "androidx.fragment:fragment-ktx:$fragment_version"
        // Testing Fragments in Isolation
        debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
    }
    
    1. Then simple code to replace the fragment,

    In your activity

    supportFragmentManager.commit {
        replace(R.id.frame_layout, YourFragment.newInstance(), "Your_TAG")
        addToBackStack(null)
    }
    

    References

    Check latest version of Fragment extension

    More on Fragments

    0 讨论(0)
提交回复
热议问题