Fragment回退栈
类似与Android系统为Activity维护一个任务栈,我们也可以通过Activity维护一个回退栈来保存每次Fragment事务发生的变化。
如果你将Fragment任务添加到回退栈,当用户点击后退按钮时,将看到上一次的保存的Fragment。
一旦Fragment完全从后退栈中弹出,用户再次点击后退键,则退出当前Activity。
transaction.add(R.id.right, rightFragment);
transaction.addToBackStack(null);
Fragment传递参数
1. Activity在构建Fragment的时候,向他传递参数setArguments(Bundle b); bundle可以携带基础数据类型,数组,以及序列化对象
2. Activity想xml中的Fragment传递数据
//找到xml中静态创建的Fragment
framgent = (ContentFragment)
getSupportFragmentManager().findFragmentByTag("fragment");
//findFragmentById(R.id.fragment);
3. Fragment之间传参 非常少
通过Activity作为桥梁,获取FragmentManager.findFragmentById(Tag)获取引用;
4. 任何的类都可以通过单例模式来获取数据
Android组件单例(View,Fragment,Activity,Application)都不允许构造方法私有化
必须保证该对象是单例的
Application由系统创建,已经保持了单例
Activity由系统创建,也保持了单例
但是Fragment由自己创建,并没有保持单例
步骤如下:
静态本类对象
static MainActivity instance;
公开访问方法
public static MainActivity getInstance() {
return instance;
}
在生命周期onCreate的时候 将静态本类对象赋值
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
setContentView(R.layout.activity_main);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.lesson10_backtostack.MainActivity"> <fragment
android:id="@+id/fragment2"
android:name="com.example.lesson10_backtostack.SingleFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"/> <ListView
android:id="@+id/lv"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/> <FrameLayout
android:id="@+id/fl_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5" />
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/> </LinearLayout>
frag_layout.xml
public class SingleFragment extends Fragment {
ImageView img; public static SingleFragment newInstance(int imgId){
SingleFragment fragment = new SingleFragment();
Bundle bundle = new Bundle();
bundle.putInt("imgId",imgId);
fragment.setArguments(bundle);
return fragment;
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.frag_layout, null);
img = (ImageView) layout.findViewById(R.id.img);
return layout;
} @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState);
Bundle bundle = getArguments();
if (bundle == null) {
img.setImageResource(R.mipmap.ic_launcher);
} else {
int imgId = bundle.getInt("imgId");
img.setImageResource(imgId);
}
img.setScaleType(ImageView.ScaleType.FIT_XY); //Fragment条用Activity的方法
MainActivity activity = (MainActivity) getActivity();
activity.showToast("这是Fragment调用Activity的方法");
} public void showToast(String msg) {
Log.e("TAG",msg);
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
}
}
SingleFragment.java
public class MainActivity extends AppCompatActivity { int[] imgId = {R.mipmap.img01,R.mipmap.img02,R.mipmap.img03,R.mipmap.img04,R.mipmap.img05};
String[] name = {"朴海镇","宋慧乔","宋仲基","全智贤","杨幂"}; ListView lv;
SingleFragment fragment;
List<String> mList = new ArrayList<>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化数据
initData();
//初始化视图
initView();
initEvents();
SingleFragment fragment2 = (SingleFragment) getSupportFragmentManager().findFragmentById(R.id.fragment2);
fragment2.showToast("这是MainActivity调用Fragment的方法"); } private void initEvents() {
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
createFragment(position); FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl_fragment,fragment);
ft.addToBackStack(null);
ft.commit();
}
});
} private void createFragment(int position) {
fragment = SingleFragment.newInstance(imgId[position]);
/*Bundle bundle = new Bundle();
bundle.putInt("imgId",imgId[position]);
fragment.setArguments(bundle);*/ } private void initView() {
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return mList.size();
} @Override
public Object getItem(int position) {
return mList.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = new TextView(getBaseContext());
}
TextView tv = (TextView) convertView;
tv.setPadding(20,20,20,20);
tv.setText(mList.get(position));
return tv;
}
});
createFragment(0);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fl_fragment,fragment);
ft.commit(); } private void initData() {
for(int i=0;i<name.length;i++){
mList.add(name[i]);
}
} public void showToast(String msg){
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
} }
MainActivity.java