How to implement the Android ActionBar back button?

后端 未结 12 2153
天涯浪人
天涯浪人 2020-11-29 15:38

I have an activity with a listview. When the user click the item, the item \"viewer\" opens:

List1.setOnItemClickListener(new OnItemClickListener() {
    @Ov         


        
12条回答
  •  -上瘾入骨i
    2020-11-29 16:12

    Selvin already posted the right answer. Here, the solution in pretty code:

    public class ServicesViewActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // etc...
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }
    }
    

    The function NavUtils.navigateUpFromSameTask(this) requires you to define the parent activity in the AndroidManifest.xml file

    
        
    
    

    See here for further reading.

提交回复
热议问题