Issue
I am in the process of building an app and have a navigation menu in my first activity, however, when I go to the second it doesn't show, so which bits of code do I copy from my first activity for the navigation menu to show there too?
I have tried to copy what I thought was relevant bits of code from the first activity into the second, however that second activity crashed, so I tried to remove that added code and failed to reverse it, so I imported the code from a backup to get it working again.
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
}
Here is my second 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))
}
Solution
Is is not feasible. You have to make separate navigation drawer on each activity and this is not feasible.
Instead you can use fragments, even you need to use fragments with navigation drawer if you want to keep same navigation drawer on different layouts too.
Answered By - DHAVAL A.
Answer Checked By - Timothy Miller (JavaFixing Admin)