add action bar with back button in preference activity

前端 未结 4 2080
遇见更好的自我
遇见更好的自我 2021-01-04 10:00

Here is my preference activity:

package com.example.hms.test;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class PrefsAct         


        
4条回答
  •  孤独总比滥情好
    2021-01-04 10:23

    You should do couple of things:

    1. Add the following to your onCreate of PreferenceActivity:

      getSupportActionBar().setDisplayShowHomeEnabled(true);
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      
    2. Override onOptionsItemSelected in PreferenceActivity:

      @Override
       public boolean onOptionsItemSelected(MenuItem item) {
           switch (item.getItemId())
           {
               case android.R.id.home:
                   NavUtils.navigateUpFromSameTask(this);
                   return true;
           }
           return super.onOptionsItemSelected(item);
       }
      
    3. change the tag in manifest for your PreferenceActivity to look something like this:

      
        
      
      
    4. Finally put android:launchMode="singleTop" in your MainActivity tag in manifest:

      
        
          
      
          
        
      
      

提交回复
热议问题