这使用导航抽屉,并使用Tab,并使用Fragment。 FragmentActivity和Fragment之间的沟通方式如何?

时间:2018-06-14 13:13:42

标签: android android-fragments

MainActivity.java

公共类MainActivity扩展了AppCompatActivity {

DrawerLayout myDrawerLayout;
NavigationView myNavigationView;
FragmentManager myFragmentManager;
FragmentTransaction myFragmentTransaction;

@覆盖     protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myDrawerLayout =(DrawerLayout)findViewById(R.id.drawerLayout);

myNavigationView = (NavigationView) findViewById(R.id.nav_drawer) ;
myFragmentManager = getSupportFragmentManager();
myFragmentTransaction = myFragmentManager.beginTransaction();
myFragmentTransaction.replace(R.id.containerView, new HomeFragment()).commit();

myNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){

@Override
public boolean onNavigationItemSelected(MenuItem selectedMenuItem) {

myDrawerLayout.closeDrawers();

if (selectedMenuItem.getItemId() == R.id.nav_item_select1) {
  FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
  fragmentTransaction.replace(R.id.containerView, new Select1()).commit();
            }

返回false;

Pyeondo.java

2 个答案:

答案 0 :(得分:0)

您已经有片段实例

fragment.setArguments(bundle);

你要在那个实例中设置参数,所以不要创建新的,使用它

fragmentTransaction.replace(R.id.containerView, fragment).commit();

答案 1 :(得分:0)

您必须使用相同的片段来设置包。

用片段

替换新的select1()
 FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        bundle.putString("Slocale1",SbtnsLocale.getText().toString());
        bundle.putString("Slocale2",SbtneLocale.getText().toString());
        bundle.putString("Sdate1",StxtsDate.getText().toString());
        bundle.putString("Sdate2",SStxtsDate.getText().toString());
        bundle.putString("Sseat",StxtSeat.getText().toString()); // use as per your need

        fragment.setArguments(bundle);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(R.id.containerView, fragment).commit();