我有一个带有两个片段的活动。这里我以编程方式在第二个片段中提供一个后退按钮以返回到第一个片段。
这是我的片段1代码:
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
V = inflater.inflate(R.layout.lm_fragement, container, false);
listview = (ListView)V.findViewById(R.id.listView1);
url="http://victorysites.org/apps/SSPC/homeinfo.php?UDID=12341234";
JsonParsing json=new JsonParsing();
list=json.getData(url);
System.out.println("length :"+list.size());
listview.setAdapter(new MyAdapter(getActivity(),list));
final ProgressDialog waitdialog;
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Bundle data = new Bundle();
data.putString("name",""+arg2);
Fragment f;
f = new PM_Fragment();
f.setArguments(data);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
Button bt=(Button)V.findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle data = new Bundle();
data.putString("name","Venkat");
Fragment f;
f = new PM_Fragment();
f.setArguments(data);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
return V;
}
}
这是我的fragment2代码:
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
f=new PM_Fragment();
V = inflater.inflate(R.layout.pm_fragment, container, false);
Bundle bundle = this.getArguments();
String myInt = bundle.getString("name");
System.out.println("NAME"+myInt);
TextView tv=(TextView)V.findViewById(R.id.portrait_message);
tv.setText(myInt);
Button bt=(Button)V.findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*Fragment f=new PM_Fragment();
getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();*/
//getActivity().getFragmentManager().beginTransaction().remove(this).commit();
getFragmentManager().popBackStack();
}
});
return V;
}
}
它工作正常,但问题是它重新加载上一个(第一个片段)屏幕。但我不想重新加载页面,我希望它像模拟器中的默认后退按钮一样工作。
任何人都可以给我一个想法。提前致谢。