I want to set toolbar on my activity which extends FragmentActivity. I know that for use setSuppoertActionBar(toolbar)
method we extends AppCompatActivity
instead of FragmentActivity
but I override the onMenuItemSelected(int featureId, MenuItem item)
method which is final in AppCompatActivity
and final method cannot override. so I'm restricted to extends FragmentActivity
.
Here is my code:
public class MainActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar); -> error is here
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()){
case R.id.action_search:
onSearchRequested();
break;
}
return super.onMenuItemSelected(featureId, item);
}
I saw many answers related to that question but everyone says extends AppCompatActivity
instead of FragmentActivity
but I want to set toolbar as well as override onMenuItemSelected(int featureId, MenuItem item)
method.
what should I do, please help.
onOptionsItemSelected
yet? – Chappell