从活动中启动片段

时间:2018-12-07 19:36:06

标签: java android android-fragments

我正在尝试从主要活动中启动片段,但是每当我尝试启动该片段时,它只会将片段上的内容覆盖在主要活动上。 这是mainActivity.java

if(passwordEntered.equals(true)&& userNameEntered.equals(true))
                {
                    if(passwordString.equals("theadmin")&&usernameString.equals("admin"))
                    {
                        Intent adminIntent=new Intent(MainActivity.this,EmptyActivity.class);
                        startActivity(adminIntent);
                    }
                }

这是adminpage.java,但它应该运行它的homefragment,但是它覆盖了mainActivity

public static FragmentManager fragmentManager;
    public static MyAppDatabase myAppDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager=getSupportFragmentManager();
        myAppDatabase= Room.databaseBuilder(getApplicationContext(),MyAppDatabase.class,"userSdb").allowMainThreadQueries().build();

        if(findViewById(R.id.fragmentContainer)!=null)
        {
            if(savedInstanceState!=null)
            {
                return;
            }
            fragmentManager.beginTransaction().add(R.id.fragmentContainer,new HomeFragment()).commit();
        }
    }

0 个答案:

没有答案