venichhe
venichhe

Reputation: 37

Fragments within ActionBarActivity

I've searched hours for examples of multiple intents and Fragments within ActionBarActivity. I am learning android programming from Deitel series and other well informative resources. The obstacles which I'm facing are to start with a simple "Hello World" program from two intents using Fragments, but the resources I'm learning from doesn't contain examples extending ActionBarActivity but they all extend Activity.

The roadblock for me is inflating fragments from ActionBarActivity using its available methods. If i can acheive this simple task, my learning curve would excel immenesly.

Upvotes: 1

Views: 2986

Answers (2)

SeahawksRdaBest
SeahawksRdaBest

Reputation: 886

Here is what I think you don't understand.

1. ActionBarActivity vs Activity

Don't worry about ActionBarActivity vs Activity. ActionBarActivity extends Activity so all the code that is valid in Activity is present in AcctionBarActivity. Please check this link.

2. Inflation

Usually what I do is make a separate class for each of my fragments as such,

public class Fragment1 extends Fragment{
// in the onCreateView I create a rootView & inflate.
final View rootview = inflater.inflate(R.layout.your_layout_file, container,false);
// Now you can use rootView to call all your Activity functions. Such as findView 
//onClick etc.
return rootview;

3. In the MainActivity

This purely depends on the way you want to structure your app. But certainly you must have a getCount and getItem function. Like so.

@Override
public Fragment getItem(int i){
    switch (i) {
        case 0:
            return new Fragment1();

        case 2:
           return new Fragment2();
        }
}

@Override
public int getCount() {
    return 2;
} 

Upvotes: 1

joao2fast4u
joao2fast4u

Reputation: 6892

ActionBarActivity extends FragmentActivity, which extends Activity. So, everything that Activity does, ActionBarActivity does it too.

ActionBarActivity provides access and control for the ActionBar, while FragmentActivity provides a FragmentManager, that is used to manage your Fragments.

ActionBarActivity provides you all these features.

There are lots of examples on the web and this site is the right place to start.

Upvotes: 2

Related Questions