我从片段来到活动课,现在我想回到我之前的片段。但片段来自抽屉布局。我无法理解该做什么.... <登记/> 这是我的代码。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_answered, container, false);
GridView answeredGridView = (GridView) view.findViewById(R.id.answeredQuestiongridview);
answeredImageAdapter = new AnsweredImageAdapter(getActivity());
answeredImageAdapter.notifyDataSetChanged();
answeredGridView.invalidateViews();
answeredGridView.setAdapter(answeredImageAdapter);
answeredImageAdapter.notifyDataSetChanged();
answeredGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity()," "+position,Toast.LENGTH_SHORT).show();
questionposition = position;
Intent intent = new Intent(getActivity(), AnswerDetailsActivity.class);
intent.putExtra("Image_position",position);
intent.putExtra("question_id",id);
startActivity(intent);
getActivity().finish();
}
});
return view;
}
现在假设我在此活动页面中。并且想要回到我的片段页面。我写了下面的代码并且不幸地停止面对应用程序。
private void answerFragment() {
answeredFragment = new AnsweredFragment();
fragmentManager = getSupportFragmentManager();
//answeredFragment = fragmentManager.findFragmentById("myFragment");
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.main_fragment_container,answeredFragment);
fragmentTransaction.commit();
}
答案 0 :(得分:1)
首先,您所遵循的方法是错误的。碎片与特定活动相关联。从您的代码中可以看出,在您的 XYZActivity 中,您拥有 AnsweredFragment 。点击答案即可 AnswerDetailsActivity 。如果您以相同的方式执行此操作,则只需从此处删除Crypt::PGPSimple
来电 -
getActivity().finish();
在这种情况下,在您的返回(硬件/软件)按下,您将能够加载您之前的片段。 :)