Susan
Susan

Reputation: 41

Action Bar as Fragment

I have just started developing an Android app and have no experience at all. I have read a lot about Activities / Fragments / Widgets, but don't seem to find a clear answer to my question which is:

Can I create the Action Bar for the app as a fragment so whenever I change an activity I will simply call the one action bar (i.e. the one fragment)? I intend to develop a dynamic UI to create fragments for individual option and thought that it would be easy to have a general Action Bar appearing on all pages.

Upvotes: 4

Views: 203

Answers (4)

Waza_Be
Waza_Be

Reputation: 39564

When you want to customize the ActionBar in all your Activities, the first step is to create a custom Theme in XML.

In this theme, you can customize nearly everything

Please refer to this excellent blog post: http://android-developers.blogspot.be/2011/04/customizing-action-bar.html

Using a Fragment for the ActionBar would be crazy!

If you want to add some code programatically in all your Activities, simply extends a custom Activity, for instance MyCustomActivity, that extends Activity.

public class MyCustomActivityextends Activity{

In this class, you can use getActionBar() and tweak it according to your needs

Upvotes: 2

IanB
IanB

Reputation: 3489

In most cases this would be an unnecessary complication. Once you get started, you will probably find that just a few simple lines of code will create an "identical" Action Bar in each activity .Themes etc can be added later. Better questions at this stage might be should I use Action Bar Sherlock to enable better support of old devices ? and you should think about the overall structure of your app e.g activities / fragment activities / fragments / tabs so that you are able to get something working quickly that can be readilly extended as you develop you full solution.

Upvotes: 0

Plastic Sturgeon
Plastic Sturgeon

Reputation: 12527

I think what you're thinking of is this: You want your action bar to be the same on every screen, and only need to program it once. The approach I use, is that the actionbar live in a root activity, containing a single viewpager. And all of the screens that the user interacts with are Fragments in that view pager.

If you create a blank android project in eclipse, and select actiobar with tabs, the project will set this up for you and you can see how that works.

Upvotes: 0

Steve Benett
Steve Benett

Reputation: 12933

If you wanna have one ActionBar for all your Activities use inheritance. Create an Activity which simply handles the ActionBar like you wanna have and make it the Superclass like this.

public class ActionBarActivity extends Activity{

    public void onCreate(... ) {
    ActionBar actionBar = getActionBar();
    // + some other method calls of your choice
    }

    public onCreateOptionsMenu(Menu menu){
        // create your actionbaritems here
    }

    public boolean onOptionsItemSelected(MenuItem item) {
         // handle your click events for the items here
    }
}

Now you can use this Activity for all your Activities with inheritance:

public class MyActivity extends ActionBarActivity{
...
}

With this setup you are free to use Fragments as you like.

Keep in mind that every time you call a new Activity the callbacks of the super class will be invoked.

Upvotes: 0

Related Questions