0xSina
0xSina

Reputation: 21553

Android activity and fragments

I've setup a new Android project that comes with an activity. Here's the boiler plate code:

if (savedInstanceState == null) {
    getFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment())
            .commit();
}

Can someone explain what this is doing exactly? From what I can see, it checks if the activity hasn't been initliazed and then inflates the layout. But what I don't understand is beginTransaction(), ew PlaceholderFragment(), and commit()

Thanks.

Upvotes: 0

Views: 102

Answers (5)

Yash Krishnan
Yash Krishnan

Reputation: 2785

//Check whether we're recreating a previously destroyed instance
if (savedInstanceState == null) {

//Execute a transaction, replacing any existing fragment with this one inside the frame.
//Getting FragmentManager object which will control fragment acvitiy
  FragmentManager fm = getFragmentManager()

//Starting a FragmentTransaction which will handle transaction to this fragment activity
  FragmentTransaction ft = fragmentManager.beginTransaction();

//Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity. 
  ft.add(R.id.container, new PlaceholderFragment());

//Schedules a commit of this transaction.
  ft.commit();
}

There is a good explanation to fragment activity here, here and here

Upvotes: 1

Attiq ur Rehman
Attiq ur Rehman

Reputation: 475

It is simple my friend. In simple Coding Language:

if (savedInstanceState == null) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.tab5, new PlaceholderFragment());
    fragmentTransaction.commit();
}

If you think that it explain everything, then it is a pleasure for me. Otherwise just ping me to add theory information.

Upvotes: 0

Atul O Holic
Atul O Holic

Reputation: 6792

As fragments are the way to go, replacing all of many heavy Activites, Eclipse has also adapted to this change which cause the boiler alert. :)

Starting a fragment (which cant be done via Intents) is treated as a transaction just like in database (not a good example i guess).

getFragmentManager() - gets the Activities FragmentManger which is responsible to initiate FragmentTransaction.

beginTransaction() - creates a new Transaction for this particular fragment job.

new PlaceholderFragment() - is an instance of the PlaceholderFragment which you can find if scroll more in the Activity.

commit - a way to commit this trasaction and bring it to effect.

See Android docs. for more details. :)

Upvotes: 0

ucsunil
ucsunil

Reputation: 7494

FragmentManager is a class which helps in managing the fragments that an activity may need. So here you are basically getting an instance of it and you are beginning a transaction. You need an instance of transaction because it lets the runtime know that some change is going to happen when this is called. Here 'add()' is that change and finally you commit it to save that change.

The arguments to add are the layout where the fragment needs to be added and the PlaceHolderFragment() is the name of the Fragment you need to put in.

Upvotes: 0

Breadbin
Breadbin

Reputation: 419

You use fragment transactions to add / replace (etc) fragments within a FrameLayout (R.id.container) and new PlaceholderFragment is a new instance of a fragment to be put into the container

Upvotes: 1

Related Questions