How to find out the identifier of a line item in a menu, knowing its decimal value?

I am using android-support-v7-appcompat.

In action, I want to show the back button in the action bar. I AM:

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 works break; default: return super.onOptionsItemSelected(item); } return true; } 

If I use a numerical filter by id it works, but I think that the ID is generated by R and therefore can change, so R.id. is used. Any idea?

+4
source share
1 answer

The home / back icon in the action bar has the identifier android.R.id.home . You can find this identifier.

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; } 
+12
source

Source: https://habr.com/ru/post/1500247/


All Articles