我是安卓新手。我知道也许我的问题是重复的,但我无法解决我的问题。我有一项活动和一些片段。当点击一个按钮时,我在这个 P 片段中处理 10 个按钮。我想管理后退按钮,我使用此代码进行管理。但我不知道我什么时候在 P 片段中,我该如何管理后退按钮?我怎样才能将我的片段添加到我的堆栈中???
活动
public class MainActivity extends FragmentActivity {
public Stack <Fragment> mFragmentStack=new Stack <Fragment>();
@Override
public void onCreate(Bundle savedInstanceState) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnProduct.setOnClickListener(onClickListener);
private OnClickListener onClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
switch(v.getId()){
case R.id.btn3:
Fragment product=new Product();
Log.e("button", "3Click");
ft.setCustomAnimations(R.anim.push_right_in, R.anim.push_left_out);
//ft.replace(R.id.frameLayout, new Product());
ft.replace(R.id.frameLayout, mFragmentStack.push(product));
ft.commit();
break;
}
}
};
@Override
public void onBackPressed(){
mFragmentStack.pop();
if(mFragmentStack.size()>0){
FragmentTransaction fTransaction=getSupportFragmentManager().beginTransaction();
fTransaction.setCustomAnimations(R.anim.push_right_in, R.anim.push_left_out);
fTransaction.replace(R.id.frameLayout, mFragmentStack.peek());
fTransaction.commit();
}else{
super.onBackPressed();
}
}
}
P.java:
public class P extends Fragment implements AnimationListener{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view=inflater.inflate(R.layout.product_view, container, false);
return view;
}
private OnClickListener onClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
switch(v.getId()){
case R.id.btn1_product:
ft.setCustomAnimations(R.anim.slide_down, R.anim.slide_up);
ft.replace(R.id.frameLayout, new CustomizedDevelopment());
ft.commit();
break;
}
}
};
}
我想要当我在 P 片段中单击后退按钮时,后退按钮显示 P 片段中的上一页。
感谢您的帮助,并为我糟糕的英语感到抱歉。