我有一个带有两个片段的Activity。我在第二个片段中以编程方式提供一个后退按钮,以goback为第一个片段。
这里是我的fragment1代码:
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;
}
}
它工作正常,但问题是它重新加载了前一个(第一个片段)屏幕。但我不想重新加载页面,我希望它在模拟器中像默认后退按钮一样工作。
任何人都可以给我一个想法。提前谢谢。
答案 0 :(得分:1)
在您的应用中使用片段时,单个 FragmentTransaction
对象可能表示应添加到后端堆栈的上下文更改。如果您通过交换片段在手机上实现 master/detail
流,则应确保在详细信息屏幕上按 Back button
返回用户到主屏幕。为此,请在提交事务之前调用addToBackStack():
// Works with either the framework FragmentManager or the
// support package FragmentManager (getSupportFragmentManager).
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();
*当后台堆栈上有 FragmentTransaction
个对象并且用户按下 Back button
时, {{1} 从后台堆栈中弹出最近的事务并执行反向操作(例如,如果事务添加了片段,则删除片段)。*
注意:FragmentManager
如果您的应用程序更新其他用户界面元素以反映片段的当前状态(例如操作栏),请记住在提交事务时更新UI。除了提交事务之外,您应该在后台堆栈更改后更新用户界面。您可以通过设置FragmentManager.OnBackStackChangedListener来监听FragmentTransaction何时被恢复:
You should not add transactions to the back stack when the transaction is for horizontal navigation (such as when switching tabs) or when modifying the content appearance (such as when adjusting filters). For more information, about when Back navigation is appropriate, see the Navigation design guide.
请参阅此link了解详情