Android App Development For Dummies
Book image
Explore Book Buy On Amazon

An instance of the android.app.FragmentManager class takes care of your app’s fragments. For example, in the code below, the manager’s replace method changes the fragment that’s shown on the user’s screen. The manager also helps you fiddle with your activity’s back stack.

package com.allmycode.frag;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ComponentNamesFragment extends ListFragment {
final static String[] COMPONENTS = { "Activity",
"Service", "BroadcastReceiver", "ContentProvider" };
@Override
public void onActivityCreated
(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, COMPONENTS));
}
@Override
public void onListItemClick(ListView l, View v,
int index, long id) {
//Create fragment with index
DocsFragment docsFragment = new DocsFragment();
Bundle args = Helper.getBundleWithIndex(index);
docsFragment.setArguments(args);
//Clear the back stack
FragmentManager fragmentManager =
getFragmentManager();
int backStackEntryCount =
fragmentManager.getBackStackEntryCount();
for (int i = 0; i < backStackEntryCount; i++) {
fragmentManager.popBackStackImmediate();
}
//Perform the transaction
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.docs, docsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}

Activities pile up on top of one another with successive startActivity calls. When the user presses Back, Android pops an activity off the stack. The most recently added activity is the first to be popped. It’s as if Android, the boss, has an agreement with members of the Activities Union. Android fires activities in reverse order of seniority.

With the introduction of fragments in Android 3.0, an activity can have its own private stack. You can display fragment A and then call fragmentTransaction.replace and fragmentTransaction.addToBackStack. The combination of method calls makes fragment B overwrite fragment A. When the user presses Back, fragment B goes away, and fragment A returns to its place on the activity’s screen. Android doesn’t destroy an entire activity until the activity has no fragments that it can jettison.

In the Clear the back stack part of the code above, the fragment manager does some quick housekeeping of the activity’s fragment stack.

When you call addToBackStack, you have the option of supplying a name for the entry that you’re putting on the back stack. If you supply null as the argument to the addToBackStack call, then the entry is unnamed. If you supply a string at that time, later in the code, you can retrieve the entry by calling FragmentManager.findFragmentByTag.

About This Article

This article can be found in the category: