我有一个片段类。在该类中,我可以通过单击gridview项来转到另一个片段类。如何按后退按钮进入原班级。
Ex:TheaterFragment类---> Gridview ---->点击gridview中的项目-----> TheaterDetailsFragment
我想按下TheaterDetailsFragment中的后退按钮,再次进入TheaterFragment类。
提前致谢
TheaterFragment类
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;
}
}
答案 0 :(得分:2)
由于这解决了您的问题,我会将其作为答案发布 -
替换
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detail)
.commit();
与
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detail)
.addToBackStack("back") //add this
.commit();
这是因为(来自API Guides)
执行此类片段事务时,您还可以将其添加到由活动管理的后台堆栈中 - 活动中的每个后台堆栈条目都是发生的片段事务的记录。后向堆栈允许用户通过按“返回”按钮来反转片段事务(向后导航)。
答案 1 :(得分:0)
这里有一些文档: http://developer.android.com/training/implementing-navigation/temporal.html
但实质上,在调用commit()之前添加行
.addToBackStack(null)
null是堆栈的可选名称。