ActionBar上的Android Back Arrow图标无效

时间:2015-12-02 20:49:34

标签: android android-actionbar

由于某些原因,此活动在操作栏中有后退箭头,但单击时它没有反应,甚至看不到识别点击。我有其他类似的活动,虽然工作正常。以下是删除了一些内容的代码:

public class LanguageActivity extends ActionBarActivity {
        private static final String TAG = "LanguageActivity";

        @InjectView(R.id.listView)
        RecyclerView mRecyclerView;

        @State
        String selectedLang;

        LangViewHolder selectedHolder = null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Icepick.restoreInstanceState(this, savedInstanceState);
            setContentView(R.layout.recyclerview);
            ButterKnife.inject(this);

            Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            setTitle(R.string.ad_title_settings_lang);

            selectedLang = PreferencesFacade.getInstance().getCurrentLang();

            mRecyclerView.setLayoutManager(new TrueWrapContentLinearLayoutManager(this));

            List<Pair<String, String>> langList = new LinkedList<>();

            langList.add(Constants.Languages.US);
            langList.add(Constants.Languages.LATIN_AMERICA_SPANISH);
            mRecyclerView.setAdapter(new RecycleViewMappedArrayAdapter(R.layout.view_language_item, new LangViewHolder(mRecyclerView), langList));
        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            Icepick.saveInstanceState(this, outState);
            super.onSaveInstanceState(outState);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()){
                case android.R.id.home:
                    onBackPressed();
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public void finish() {
            super.finish();
            PreferencesFacade.getInstance().setCurrentLang(selectedLang);
            Log.v(TAG, "Finishing?");
        }
        public class LangViewHolder extends RecycleViewMappedArrayAdapter.ViewHolder<Pair<String, String>>{
   ...
        }
    }

清单摘录:

    <activity android:name=".activities.LanguageActivity"
        android:parentActivityName=".activities.MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MainActivity"/>
    </activity>

2 个答案:

答案 0 :(得分:1)

为什么不扩展AppCompatActivity(我认为actionbaractivity已弃用...) ?

工具栏工具栏=(工具栏)findViewById(R.id.the_id);

setSupportActionBar(栏);

getSupportActionBar()setDisplayHomeAsUpEnabled(真);

//试试这个

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });

答案 1 :(得分:1)

这似乎对我有用:

Toolbar tBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tBar); // sets the Toolbar as the actionbar 

tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp));

tBar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            onBackPressed(); // calls the System onBackPressed method
        }

    });

更新:

getDrawable(int id)已弃用,因此您应更改此行代码:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP){ 

   tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp, getApplicationContext().getTheme()));

} else {

   tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp));

}