Fragment栈
我们知道Android系统提供了一个任务栈用于保存Activity,同样也为Fragment提供了一个回退栈,如果我们把Fragment压入回退栈后点击返回可以显示上一个Fragment.
看下APIDemo下的实例

布局fragment_stack.xml
[java]<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:padding="4dip" >
<Button
android:id="@+id/new_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Push" >
<requestFocus />
</Button>
<Button
android:id="@+id/delete_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Pop" >
</Button>
</LinearLayout>
<FrameLayout
android:id="@+id/simple_fragment"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>[/java]
代码:
[java]public class FragmentStack extends Activity {
int mStackLevel = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_stack);
Button button = (Button) findViewById(R.id.new_fragment);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mStackLevel++;
addFragmentToStack();
}
});
button = (Button) findViewById(R.id.delete_fragment);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mStackLevel–;
getFragmentManager().popBackStack(); //弹栈
}
});
}
void addFragmentToStack() {
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
public static class CountingFragment extends Fragment {
int mNum;
private static int[] sColorValue = new int[] {
android.R.color.holo_blue_light,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light };
/**
* Create a new instance of CountingFragment, providing "num" as an
* argument.
*/
static CountingFragment newInstance(int num) {
CountingFragment f = new CountingFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance’s number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment’s UI is just a simple text view showing its instance
* number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_stack_layout, container, false);
View tv = v.findViewById(R.id.text);
((TextView) tv).setText("Fragment #" + mNum);
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
Log.v("TAG", "mNum % 3 " + mNum % 3);
tv.setBackgroundColor(getActivity().getResources().getColor(sColorValue[mNum % 3]));
return v;
}
}
}
[/java]
压栈代码
[java]FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.addToBackStack(null);[/java]
弹栈代码
[java]getFragmentManager().popBackStack();[/java]
转载请注明来源:Fragment栈


























