具有android.intent.action.SEND的导航架构组件

时间:2019-01-02 17:42:35

标签: android android-studio kotlin android-architecture-components

我正在使用导航体系结构组件,我想从抽屉中打开共享意图吗?

MainActivity类:AppCompatActivity(){

/*   override fun onNavigationItemSelected(item: MenuItem): Boolean {
       when (item.itemId) {
           R.id.action_share_app -> {

               val sendIntent: Intent = Intent().apply {
                   action = Intent.ACTION_SEND
                   putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
                   type = "text/plain"
               }
               startActivity(sendIntent)
               return true
           }

       }

       return true
   }

* /     重写fun onCreate(savedInstanceState:Bundle?){

    setTheme(R.style.AppTheme_NoActionBar)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)

// navigationView.setNavigationItemSelectedListener(this)         setupNavigation()

}

private fun setupNavigation() {
    val navController = findNavController(this, R.id.nav_host_fragment)

    // Update action bar to reflect navigation
    setupActionBarWithNavController(this, navController, drawerLayout)


    // Handle nav drawer item clicks
    ActivityNavigator(this)
        .createDestination().intent = Intent().apply {
        action = Intent.ACTION_SEND
        putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
        type = "text/plain"
    }

    navigationView.setNavigationItemSelectedListener { menuItem ->
        when (menuItem.itemId) {
            R.id.action_share_app -> {
                val sendIntent: Intent = Intent().apply {
                    action = Intent.ACTION_SEND
                    putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
                    type = "text/plain"
                }
                startActivity(sendIntent)
            }
        }

        menuItem.isChecked = true
        drawerLayout.closeDrawers()
        false
    }

    // Tie nav graph to items in nav drawer
    setupWithNavController(navigationView, navController)
}

override fun onSupportNavigateUp(): Boolean {
    return navigateUp(findNavController(this, R.id.nav_host_fragment), drawerLayout)
}

0 个答案:

没有答案