0

I came to the activity class as for need from an fragment and now I want to go back to my earlier fragment.But The fragment was came from an drawer layout.I can't understand what to do....
here is my code.

@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;
}

Suppose now I am in this activity page. and want to go back to my fragment page..I wrote following code and facing the app is unfortunately stopped.

 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();
    }
opu opu
  • 55
  • 6

1 Answers1

1

First of all the approach you are following is wrong. Fragments are tied to specific activities. From your code, it seems that in your XYZActivity, you have the AnsweredFragment. On click of an answer you are going to AnswerDetailsActivity. If you are doing it in the same way, then just remove getActivity().finish(); call from here -

 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();


    }
});

In that case, on your Back(Hardware/Software) press you will be able to load your earlier fragment. :)

Abhishek
  • 920
  • 13
  • 23
  • Thank You I remove getActivity().finish(); – opu opu Dec 09 '15 at 05:54
  • Glad to help you. If you can, please mark my answer as a correct answer. So, that other people can get guided by my answer. Thanks. :) – Abhishek Dec 09 '15 at 06:02
  • Brother I solved my problem without going to the fragment from activity but I also want to know that is that possible to go back to the fragment from activity that activity comes from fragment? – opu opu Dec 09 '15 at 06:11