我有2个片段A和B.首先打开一个片段,有一个按钮。 onButtonClick
它转到片段B.在片段B中,用户选择一些语言或其他东西,onButtonClick
来到片段A.所以当谈到片段A并且我点击电话时,它会进入再回到片段B.我希望当片段B中的用户选择一些信息,语言和其他东西,然后点击下一个按钮时,它就会被破坏。像这样的东西。当它从A到B时,然后在选择信息从B到A之后我想要破坏B片段。在片段中,我无法覆盖onBackPressed
方法,因为这是针对活动的。有任何想法吗?在互联网上找到一些样本,但他们没有帮助。如果您需要代码请说。谢谢你的帮助。
好的,这是片段A。
public class PersonalInfoFragment extends BaseFragment {
View mainView;
public static final String TEXT = "TEXT";
TextView dots;
EditText skills_description;
Button clear, save, languages;
CircleImageView circleImageView1, circleImageView2, circleImageView3,
circleImageView4, circleImageView5, circleImageView6;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.personal_info, container, false);
init(mainView);
Bundle bundle = getArguments();
if (bundle != null) {
ArrayList<Integer> s = bundle.getIntegerArrayList("IMAGE");
if (s != null) {
for (int i = 0; i < s.size(); i++) {
switch (i) {
case 0:
circleImageView1.setImageResource(s.get(i));
break;
case 1:
circleImageView2.setImageResource(s.get(i));
break;
case 2:
circleImageView3.setImageResource(s.get(i));
break;
case 3:
circleImageView4.setImageResource(s.get(i));
break;
case 4:
circleImageView5.setImageResource(s.get(i));
break;
case 5:
circleImageView6.setImageResource(s.get(i));
break;
default:
break;
}
}
}
}
return mainView;
}
private void init(View v) {
skills_description = v.findViewById(R.id.skills);
clear = v.findViewById(R.id.clear);
languages = v.findViewById(R.id.select_language);
dots = v.findViewById(R.id.dots);
save = v.findViewById(R.id.save);
circleImageView1 = v.findViewById(R.id.language1);
circleImageView2 = v.findViewById(R.id.language2);
circleImageView3 = v.findViewById(R.id.language3);
circleImageView4 = v.findViewById(R.id.language4);
circleImageView5 = v.findViewById(R.id.language5);
circleImageView6 = v.findViewById(R.id.language6);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
skills_description.setText("");
}
});
languages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction().replace(R.id.container,
new FragmentLanguage()).addToBackStack(null).commit();
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}}
在languages.setOnClickListener
上,它会转到片段B。
languages.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction().replace(R.id.container,
new FragmentLanguage()).addToBackStack(null).commit();
}
});
这是片段B。
public class FragmentLanguage extends BaseFragment {
public static final String TEXT = "TEXT";
MyAdapter adapter;
View mainView;
ListView listView;
Button next;
PersonalInfoFragment info = new PersonalInfoFragment();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.listview, container, false);
listView = mainView.findViewById(R.id.list_view);
fillData();
showResult();
return mainView;
}
public void fillData() {
ArrayList<Item_obj> objS = new ArrayList<>();
//Array list of Languages
objS.add(new Item_obj("Armenian", R.drawable.download, false));
objS.add(new Item_obj("Russian", R.drawable.russian, false));
objS.add(new Item_obj("US English", R.drawable.usa, false));
objS.add(new Item_obj("Portugal", R.drawable.france, false));
objS.add(new Item_obj("Spanish", R.drawable.italy, false));
objS.add(new Item_obj("Georgian", R.drawable.download, false));
objS.add(new Item_obj("French", R.drawable.download, false));
objS.add(new Item_obj("Italian", R.drawable.download, false));
adapter = new MyAdapter(getActivity(), objS);
listView.setAdapter(adapter);
}
public void showResult() {
next = mainView.findViewById(R.id.next);
clear = mainView.findViewById(R.id.clear_lang);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Item_obj> it = adapter.getCheck();
Bundle bundle = new Bundle();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < adapter.getCheck().size(); i++) {
Item_obj items = it.get(i);
if (items.isSelected) {
list.add(items.image);
}
}
if (list.size() > 6) {
Toast.makeText(getActivity(), "You can't select more than 6 elements", Toast.LENGTH_SHORT).show();
list.clear();
}
bundle.putIntegerArrayList("IMAGE", list);
info.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.container, info).addToBackStack(null).commit();
listView.setAdapter(adapter);
}
});
}}
在next.setOnClickListener
中,它会再次返回到包含所选语言的片段A.
答案 0 :(得分:0)
fragmentTransaction.remove(yourfragment).commit()
添加到onBackPressed()或将意图从B设置为A
答案 1 :(得分:0)
不要从FragmentLanguage片段创建一个新的PersonalInfoFragment实例,而是按下下一个按钮,你应该通过创建一个带接口的回调并调用onBackPressed方法将bundle值传递给PersonalInfoFragment。
FragmentLanguage中的回调创建代码:
private OnNextClickListener mOnNextClickListener;
interface OnNextClickListener{
void onNextButtonClicked(Bundle bundle);
}
public void setOnNextClickListener(OnNextClickListener
mOnNextClickListener) {
this.mOnNextClickListener = mOnNextClickListener;
}
调用mOnNextClickListener.onNextButtonClicked(bundle);按下下一个按钮并在PersonalInfoFragment中实现界面。