Reputation: 1
There are three types of toolbar used in this app. (toolbar, toolbar2, toolbar3)
The MainActivity is connected to four fragments through the bottom navigation bar. (home, community, calendar, mypage)
toolbar - used in "MainActivity" (home, community, calendar, mypage)
toolbar2 - used when navigating to MainActivity -> "NoticeFragment"
toolbar3 - used when navigating to MainActivity -> CommunityFragment -> "CommunityItemFragment"
I want to put the option menu in toolbar3 only.
However, if I write onCreateOptionsMenu in the MainActivity, an option menu created only in toolbar.
How do I solve this problem?
<activity_main.xml>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"></androidx.appcompat.widget.Toolbar>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"></androidx.appcompat.widget.Toolbar>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"></androidx.appcompat.widget.Toolbar>
<MainActivity.kt>
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
private val fl: FrameLayout by lazy {
findViewById(R.id.main_frm)
}
private var backPressedTime : Long = 0
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
replaceFragment(NaviCommunityFragment())
val main_bnv = findViewById<BottomNavigationView>(R.id.main_bnv)
setSupportActionBar(toolbar)
supportActionBar?.setDisplayShowTitleEnabled(false)
var noticeitem = findViewById<ImageView>(R.id.noticeitem)
var toolbar = findViewById<Toolbar>(R.id.toolbar)
var toolbar2 = findViewById<Toolbar>(R.id.toolbar2)
var toolbar3 = findViewById<Toolbar>(R.id.toolbar3)
close_notice.setOnClickListener {
val transaction = supportFragmentManager.popBackStack()
toolbar.visibility = View.VISIBLE
toolbar2.visibility = View.INVISIBLE
toolbar3.visibility = View.INVISIBLE
}
noticeitem.setOnClickListener{
val transaction = supportFragmentManager.beginTransaction()
.replace(R.id.main_frm, NoticeFragment())
.addToBackStack(null)
transaction.commit()
toolbar.visibility = View.INVISIBLE
toolbar2.visibility = View.VISIBLE
toolbar3.visibility = View.INVISIBLE
}
btn_back.setOnClickListener {
val transaction = supportFragmentManager.popBackStack()
toolbar.visibility = View.VISIBLE
toolbar2.visibility = View.INVISIBLE
toolbar3.visibility = View.INVISIBLE
}
main_bnv.setOnItemSelectedListener { item ->
changeFragment(
when (item.itemId) {
R.id.navigation_home -> {
main_bnv.itemIconTintList = null
main_bnv.itemTextColor = null
NaviHomeFragment()
}
R.id.navigation_community -> {
main_bnv.itemIconTintList = null
NaviCommunityFragment()
}
R.id.navigation_calendar -> {
main_bnv.itemIconTintList = null
Calendar_fragment()
}
else -> {
main_bnv.itemIconTintList = null
NaviMypageFragment()
}
}
)
true
}
main_bnv.selectedItemId = R.id.navigation_home
}
private fun changeFragment(fragment: Fragment) {
supportFragmentManager.popBackStackImmediate()
supportFragmentManager
.beginTransaction()
.replace(R.id.main_frm, fragment)
.commit()
}
private fun replaceFragment(naviCommunityFragment: Fragment){
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.main_frm, naviCommunityFragment)
fragmentTransaction.commit()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.toolbar3_menu, menu)
return super.onCreateOptionsMenu(menu)
}
}
<NaviCommunityAdapter.kt>
override fun onBindViewHolder(holder: NaviCommunityViewHolder, position: Int) {
holder.title.text = itemList[position].title
holder.nickname.text = itemList[position].nickname
holder.itemView.setOnClickListener(object : View.OnClickListener{
override fun onClick(p0: View?) {
val activity=p0!!.context as AppCompatActivity
val communityItemFragment = CommunityItemFragment()
activity.supportFragmentManager.beginTransaction().replace(R.id.main_frm, communityItemFragment)
.addToBackStack(null).commit()
activity.toolbar.visibility = View.INVISIBLE
activity.toolbar3.visibility = View.VISIBLE
}
})
}
I thought that using onCreateOptionsMenu in MainActivity would have an option menu in all toolbars.
So I wanted to make the option menu only when toolbar3 is visible.
However, option menus are created only in toolbar, and not in toolbar2 or toolbar3.
Upvotes: 0
Views: 576
Reputation: 66
For my solution first you have to create a static Menu in your main activity:
public static Menu your_menu;
After that you have to saved that menu with your menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar3_menu, menu);
your_menu = menu;
return super.onCreateOptionsMenu(menu);
}
Then in your fragment if you want to show it just call it and set its visible to true
Like if you want your menu to show only in CommunityItemFragment then in that fragment set it
MainActivity.your_menu.setVisible(true);
and set it to false in other fragements or just set it false with check fragment condition in your MainActivity
Upvotes: 0