As a android A rookie , I used it the other day when I was doing the project fragment, Demand is from a Fragment Jump to another Fragment, And it's about passing data , It's like Activity The jump is the same . I haven't found any good Liezi in the Internet for a long time , Finally, through reading other people's blogs and looking up documents, I finally finished , Now sort it out , Hope to help children's shoes in need .
1, First, in the first Fragment Get it in there FragmentManger and FragmentTransaction The code is as follows .
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
fm = getFragmentManager();
}
2 Prepare the data jump to be passed .
deptListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int postion,
long arg3) {
ft = fm.beginTransaction();// be careful . One transaction can only commit once , So don't define it as a global variable
long id = adapter.getDepartments().get(postion).getId();
String name = adapter.getDepartments().get(postion).getName();
DeptDocFragment df = new DeptDocFragment();
Bundle bundle = new Bundle();
bundle.putLong("id", id);
bundle.putString("name", name);
df.setArguments(bundle);
ft.replace(R.id.guide_content, df);
ft.addToBackStack(null);
ft.commit();
}
});
above DeptDocFragment That's what I want to jump Fragment, And transfer data to Activity equally , use Bundle, Finally, it was approved df.setArguments(bundle); Carrying data . adopt replace Method switching Fragment Realize jump , Finally, don't forget commit
Submit .
3 And then there's another one Fragment Get the first one in Fragment Data passed on .
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
deptId = bundle.getLong("id");
deptName = bundle.getString("name");
}
stay Fragment Of onCreate()
Method . And then here Fragment Complete the logic you need . okay , whole Fragment The jump is complete . Hope to help children's shoes in need ...
Technology
Daily Recommendation