How to open a Fragment on button click from a fragment in Android

后端 未结 4 1695
醉梦人生
醉梦人生 2020-11-28 09:12

I have some button on my HomeFragment i want to to open a Fragment on button click from a fragment . Something like Navigation. I am trying with bellow code but did not work

相关标签:
4条回答
  • 2020-11-28 09:14

    I have a simple trick to it! This is the code in MainActivity:

    Intent intent=new Intent(getApplicationContext(),Main2Activity.class);
            intent.putExtra("pos",1);
            startActivity(intent);
    
    Intent intent1=new Intent(getApplicationContext(),Main2Activity.class);
            intent1.putExtra("pos",2);
            startActivity(intent1);
    

    In Main2Activity which is a NavigationDrawer with fragments

    Bundle extras;
      extras=getIntent().getExtras();
            if(extras!=null)
            {
                position=extras.getInt("pos");
                if(position==1)
                {
                    fragment=new FragmentOne();
    
                    if(fragment !=null)
                    {
                        android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.screen_area,fragment);
                        fragmentTransaction.commit();
                    }
                }
                if(position==2)
                {
                    fragment=new FragmentTwo();
    
                    if(fragment !=null)
                    {
                        android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.screen_area,fragment);
                        fragmentTransaction.commit();
                    }
                }
            }
    
    0 讨论(0)
  • 2020-11-28 09:21

    You can replace the fragment using FragmentTransaction on button click. Something like this:

      Fragment someFragment = new SomeFragment(); 
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter
        transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
        transaction.commit(); 
    

    Learn about performing fragment transactions here.

    code:

      package com.rupomkhondaker.sonalibank;
    
    import android.app.Fragment;
    import android.app.FragmentTransaction;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    public class HomeFragment extends Fragment implements View.OnClickListener {
    
        public HomeFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    
            Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
            Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);
    
            aboutBtn.setOnClickListener(this);
            phonebookBtn.setOnClickListener(this);
    
    
            return rootView;
        }
    
        @Override
        public void onClick(View view) {
            Fragment fragment = null;
            switch (view.getId()) {
                case R.id.aboutusButton:
                    fragment = new AboutFragment();
                    replaceFragment(fragment);
                    break;
    
                case R.id.phbookButton:
                    fragment = new PhoneBookFragment();
                    replaceFragment(fragment);
                    break;
            }
        }
    
        public void replaceFragment(Fragment someFragment) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_container, someFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-28 09:24

    Kotlin example:

    fun FragmentActivity.replaceFragment(fragment: Fragment, frameId: Int = R.id.fragment_container, addToStack: Boolean) {
    val doesFragmentAlreadyExists = supportFragmentManager.findFragmentByTag(fragment.javaClass.simpleName) != null
    if (!doesFragmentAlreadyExists) {
        supportFragmentManager.inTransaction {
            if (addToStack) replace(frameId, fragment, fragment.javaClass.simpleName)
                .addToBackStack(fragment.javaClass.simpleName)
            else
                replace(frameId, fragment, fragment.javaClass.simpleName)
        }
    }}
    
    0 讨论(0)
  • 2020-11-28 09:37

    You can use it

    NextFragment nextFrag= new NextFragment();
    getActivity().getSupportFragmentManager().beginTransaction()
                 .replace(R.id.Layout_container, nextFrag, "findThisFragment")
                 .addToBackStack(null)
                 .commit();
    
    0 讨论(0)
提交回复
热议问题