ActionBar Buttons : Unreachable statement - android

Can you please tell me what is wrong with my code ? (I bet it's something stupid but i can't find it..)
My code :
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.action_go_home:
//go home action
Intent i = new Intent(this, UserProfileActivity.class);
startActivity(i);
return true;
break;
case R.id.action_select_categories:
//select categories
return true;
case R.id.action_refresh:
//refresh timeline
return true;
default :
return super.onOptionsItemSelected(item);
}
}

You have a return statement before your switch.
You should fix it like:
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()){
....

Related

how to fix onOptionsItemSelected when Im converting an activity to a fragment

I'm converting some activities to several fragments , and now when I press back button it doesn't work.
what changes should I do in this fragment when its returning back to Previous activity and when returning to Previous fragment?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.returnHome:
Intent i= new Intent(getActivity().getApplicationContext(), WoundNavigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Try like this
write this in onCreateView() method
setHasOptionsMenu(true)
and make these changes
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.returnHome:
Intent i= new Intent(getActivity(), WoundNavigation.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
It may help. If this also doesn't works then you have to manage fragment backstack in activity from where you are calling your fragment

Three dots menu OnClick Listener

i am new at android.I am using three dots menu in my android app but when i click on item on on three dots menu..my app crashes.can someone help me here is my code:
enter code here#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
break;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
break;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
break;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
break;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.account:
Intent acc=new Intent(this,Account.class);
startActivity(acc);
return true;
case R.id.setting:
Intent seting=new Intent(this,setting.class);
startActivity(seting);
break;
case R.id.feedback:
Intent feedback=new Intent(this,feedback.class);
startActivity(feedback);
return true;
case R.id.help:
Intent help=new Intent(this,help.class);
startActivity(help);
return true;
case R.id.faq:
Intent FAQ=new Intent(this,FAQ.class);
startActivity(FAQ);
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
Seems there is nothing wrong with the code.
This could be issue with declration in manifest.
Make sure the activities account, help are declared in manifest.

Android menu crashs application

I have the following in my code and I want to switch to this new activity when I select it from the menu, but the app just keeps closing:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent("com.my.project.BAAS"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Please help, 2 days so far.....
You have to pass context and class which is to be opened.
Your code should be like this.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.mi_baas:
startActivity(new Intent(getContext(),BAAS.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Hope it helps:)

Item ID of title ActionBar

look this image:
When you click on the "Action bar" string my app enter in this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
So also it is an item, the problem is that i don't know which id it has.
Where can i get the id of this item ?
android.R.id.home
if you have set
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true)‌​;
Try this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
case android.R.id.home:
//here you put your code
Toast.makeText(this, "You click Action Bar", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How to find out the String ID of an item in the Menu knowing its decimal value?

I am using android-support-v7-appcompat.
In an activity I want to show in the actionbar the back button.
I do:
public class News extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_news_screen);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
}
}
And:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
System.out.println(item.getItemId()); // 16908332
System.out.println(R.id.home); // 2131034132
System.out.println(R.id.homeAsUp); // 2131034117
switch(item.getItemId())
{
case R.id.home:
onBackPressed();
break;
case R.id.homeAsUp:
onBackPressed();
break;
case 16908332:
onBackPressed(); // it's works
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
If I use numerical filter by id works, but I think that ID is generated by R and therefore can change, is therefore used R.id. . Any idea?
The home/back icon in the actionbar has the id android.R.id.home.
You can look for that id.
The values in android.R.* will never change and are linked statically.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.home:
onBackPressed();
break;
case R.id.homeAsUp:
onBackPressed();
break;
case android.R.id.home:
onBackPressed();
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}

Categories

Resources