简体   繁体   中英

How can I add this extra icon in toolbar for some fragments

I am making an android application which have a design like this:

https://imgur.com/R1JL7Na

So my design idea is that I will make a single Activity and all the screens will be Fragment type and I will do Fragment Transaction to change to screens or we can say fragments. This will keep same toolbar across all fragments . But some fragments, can say 4-5 fragments toolbar should have one extra icon. Like in below image.

https://imgur.com/a/LhP0irZ

The extra icon is filters icon. So how can I add this extra icon in toolbar for some fragments? Do I have to make an extra Activity with different toolbar layout ?

Please provide me some good way to achieve this

Every Fragment can has different menu.

In Fragment's onCreate method add this:

setHasOptionsMenu(true);

and after that override onCreateOptionsMenu callback in the Fragment .

In fragment

Intent intent = new Intent(context,Activity.class)
intent.putExtra("pin",pins);
System.out.println(pin);

In Activity

pined=getIntent().getStringExtra("pin");
System.out.println(pined);

在活动类的工具栏中放置一个显示/隐藏第三个按钮的方法,并在fragments访问您的activity ,并在每个fragment使用setUserVisibleHint ,您将知道要在哪个fragment上显示第三个按钮。

you can override onCreateOptionsMenu method in your class and override onOptionsItemSelected try

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
     menuInflater.inflate(R.menu.demo,menu)
    return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return super.onOptionsItemSelected(item)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM