How to keep actionbar fixed across all activities?

时光怂恿深爱的人放手 提交于 2019-12-06 05:40:57

One activity and as many fragments as you need.

the moment you call startActivity(intent); the framework will roll the activity transition animation with all its contents (including the action bar)

You can have this behaviour using only Activities. These are the methods you need in each Activity.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.button_1:
            handleButton1();
            return true;
        case android.R.id.button_2:
            handleButton2();
            return true;
           ...
        default:
           ...
    }
}

Also, you can access action bar object, in your onCreate, and customize it the way you want. For example, enabling home button etc:

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
    ...

But, if you have many activities, you probably don't want to do this in each of them. Instead, you can have this code in a Super-class activity, and then have other activities extend it. This is the preferred way - you do it in one place only.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!