CraZyDroiD
CraZyDroiD

Reputation: 7105

Getting to the previous fragment when pressing back button

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



    }

Upvotes: 0

Views: 1238

Answers (2)

user3880179
user3880179

Reputation:

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.

Upvotes: 2

Joss Stuart
Joss Stuart

Reputation: 1886

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.

Upvotes: 0

Related Questions