How to implement the Android ActionBar back button?

后端 未结 12 2160
天涯浪人
天涯浪人 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条回答
  •  一整个雨季
    2020-11-29 16:23

    Building on Jared's answer, I had to enable and implement the action bar back button behavior in several activities and created this helper class to reduce code duplication.

    public final class ActionBarHelper {
        public static void enableBackButton(AppCompatActivity context) {
            if(context == null) return;
    
            ActionBar actionBar = context.getSupportActionBar();
            if (actionBar == null) return;
    
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
    

    Usage in an activity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    
        ActionBarHelper.enableBackButton(this);
    }
    

提交回复
热议问题