0

I have a fragment class.In that class i can go to another fragment class by clicking a gridview item. How can i come to the original class by pressing the back button.

Ex :TheaterFragment class --->Gridview ---->click item in gridview-----> TheaterDetailsFragment

I want to press the back button from the TheaterDetailsFragment and come to TheaterFragment class again.

Thanks in advance

TheaterFragment class

package com.fortuna.cinemalk;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.content.Intent;
import android.widget.AdapterView;



import com.fortuna.cinemalk.adapter.LazyAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;
import com.fortuna.cinemalk.util.Element;

public class TheaterFragment extends Fragment {

    private GridView gridView;

    private ArrayList<BaseElement> filmTheater;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.theater_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        gridView = (GridView) view.findViewById(R.id.gridView1);


        gridView.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> parent, View v,
                 int position, long id) {


              android.support.v4.app.Fragment detail = new TheaterDetailFragment();
              android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit(); 
               }
              });

            /* FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment profileFragment = new MovieDetailFragment();//the fragment you want to show
        profileFragment.setArguments(bundle);
        fragmentTransaction.replace(R.id.shortfilm, profileFragment);//R.id.content_frame is the layout you want to replace
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();  */

            new BackGround().execute();

        return view;
    }


    public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            filmTheater = JSONServices.getTheater();
            return null;
        }

        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setTheater(filmTheater);

            adapter = new LazyAdapter(filmTheater, activity,
                    Element.THEATER_LIST.getType());

            gridView.setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }

} 

TheaterDetailFragment

package com.fortuna.cinemalk;

import com.fortuna.cinemalk.service.CommonVariable;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

public class TheaterDetailFragment extends Fragment {

    private Activity activity;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {



        View view = inflater.inflate(R.layout.filmhall, container,
                false);

        activity = this.getActivity();


        return view;
    }



    }
CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182
  • have you tried using `add` in place of `replace`? and You can use `backstack` –  Aug 08 '14 at 09:26
  • `http://stackoverflow.com/questions/24032956/action-bar-back-button-not-working/24080044#24080044` check out this link – Stephen Aug 08 '14 at 09:27
  • @IKmhr , there i'm using that code to go from TheaterFragment to TheaterDetailsFragment. What i want is to come back from TheaterDetailsFragment to TheaterFragment. – CraZyDroiD Aug 08 '14 at 09:34
  • Try changing `fragmentManager.beginTransaction().replace(R.id.content_frame, detail).commit();` to `fragmentManager.beginTransaction().replace(R.id.content_frame, detail).addToBackStack("back").commit();` –  Aug 08 '14 at 09:36

2 Answers2

2

Since this solved your problem, I'll post it as an answer-

Replace

fragmentManager.beginTransaction()
    .replace(R.id.content_frame, detail)
    .commit();

with

fragmentManager.beginTransaction()
    .replace(R.id.content_frame, detail)
    .addToBackStack("back")     //add this
    .commit();

This works because (from the API Guides)

When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.

  • @user3899126 You can accept the awnser (by clicking the green tick) to mark this question as resolved. –  Aug 08 '14 at 09:52
0

There is some documentation here: http://developer.android.com/training/implementing-navigation/temporal.html

But essentially, before calling commit() add the line

.addToBackStack(null)

The null is an optional name for the stack.

Joss Stuart
  • 1,856
  • 1
  • 17
  • 19