在另一个 activity 中显示连接到一个 activity 的现有导航菜单

show existing navigation menu connected to one activity in another activity

我正在构建一个应用程序,在我的第一个 activity 中有一个导航菜单,但是,当我转到第二个时,它没有显示,所以我复制了哪些代码我的第一个 activity 导航菜单也显示在那里?

我试图将我认为相关的代码从第一个 activity 复制到第二个 activity,但是第二个 activity 崩溃了,所以我试图删除添加的代码但失败了为了逆转它,所以我从备份中导入了代码以使其再次运行。

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    val fab: FloatingActionButton = findViewById(R.id.fab)
    fab.setOnClickListener { view ->
        val intent = Intent(this, Main2Activity::class.java)
        val sharedPref = this?.getPreferences(Context.MODE_PRIVATE)
        val mystr = sharedPref.getInt(getString(R.string.STR), 0)
        intent.putExtra("data", mystr)
        startActivity(intent)
    }
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val navView: NavigationView = findViewById(R.id.nav_view)
    val toggle = ActionBarDrawerToggle(
        this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close
    )
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()

    navView.setNavigationItemSelectedListener(this)

    //val db = FirebaseFirestore.getInstance()
}

override fun onBackPressed() {
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the menu; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.main, menu)
    return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    return when (item.itemId) {
        R.id.action_settings -> true
        else -> super.onOptionsItemSelected(item)
    }
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    // Handle navigation view item clicks here.
    when (item.itemId) {
        R.id.nav_home -> {
            // Handle the camera action
            val i = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/brobostigon/"))
            startActivity(i)
        }
        R.id.nav_gallery -> {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }
        R.id.nav_slideshow -> {
            val intent = Intent(this, Main2Activity::class.java)
            startActivity(intent)
        }
        R.id.nav_tools -> {

        }
        R.id.nav_share -> {
            val i = Intent(Intent.ACTION_VIEW, Uri.parse("http://taylorworld.me.uk/privacy-policy.html"))
            startActivity(i)
        }
        R.id.nav_send -> {

        }
    }
    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    drawerLayout.closeDrawer(GravityCompat.START)
    return true
}

这是我的第二个activity

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        //val mystr: Int = intent.getIntExtra("data", 0)
        //editText8.setText(Integer.toString(mystr))
    }

是不可行的。您必须在每个 activity 上制作单独的导航抽屉,这是不可行的。

相反,您可以使用片段,如果您也想在不同的布局上保留相同的导航抽屉,甚至需要将片段与导航抽屉一起使用。

val fragmentManager = supportFragmentManager val fragmentTransaction = fragmentManager.beginTransaction()
val fragment = TitleFragment() val fragment2 = MiscFragment() fragmentTransaction.add(R.id.contentlayout, fragment)
fragmentTransaction.remove(片段2) fragmentTransaction.commit()

我把它放在我的 mainactivity oncreate() 中,考虑到当 activity 加载时它会强制它加载一个特定的片段,并且只加载那个片段。 contentlayout 是将应用到的布局的 ID,即在我的例子中它是 content_main.xml.

中的 constraintlayout