How to keep actionbar fixed across all activities?

白昼怎懂夜的黑 提交于 2020-01-02 18:37:09

问题


In the Facebook app v2.3, when you navigate from one activity(or frgament?) to another, the actionbar stays fixed across all activities(or fragments?). For example, If you navigate from you news feed to someone's profile page, then the action bar stays constant, and the page below is refreshed. So my question is, is it possible to have this behavior using only activities? Or should i just have one main activity, and fragments for all sub activities?


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/15836254/how-to-keep-actionbar-fixed-across-all-activities

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