显示与另一个活动中的一个活动相关的现有导航菜单

时间:2019-06-16 14:55:12

标签: android android-activity kotlin android-navigationview

我正在构建应用程序,并且在我的第一个活动中有一个导航菜单,但是,当我转到第二个活动时,它没有显示,因此我要从我的第一个活动中复制哪些代码段?导航菜单也显示在那里?

我试图将我认为与代码相关的部分从第一个活动复制到第二个活动中,但是第二个活动崩溃了,所以我试图删除添加的代码,但未能将其反转,因此我从备份以使其恢复正常工作。

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
}

这是我的第二项活动

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))
    }

2 个答案:

答案 0 :(得分:0)

是不可行的。您必须为每个活动创建单独的导航抽屉,这是不可行的。

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

答案 1 :(得分:0)

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

我将其放在我的mainactivity oncreate()中,以为活动加载时会强制其加载特定的片段,并且仅加载该片段。 contentlayout是将要应用到的布局的ID,即在我的情况下,它是content_main.xml中的约束布局。