问题描述
我已示出其上显示在网格和数据从web服务来的产品清单。而当我在项目点击它需要新的屏幕,但从此屏幕来有网格布局屏幕时重建,但我要救样活性的实例,这样不会再次创建屏幕。我能做些什么来实现这一目标?
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
mfragmentManager = getActivity()getSupportFragmentManager()。
mhandler =新的处理程序(本);
mLvAllDeals =(GridView控件)mview.findViewById(R.id.xLvAllDeals);
mLvAllDeals.setCacheColorHint(0);
mLvAllDeals.setOnItemClickListener(本); 新MyDealsAsyncTask()执行();
} 公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
mAlDealName =新的ArrayList< AllDealsData>();
}
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
如果(arg0中== mLvAllDeals)
{
位置= ARG2; AllDealsData豆; 豆= mAlDealName.get(位置); 片段FRAG = getFragmentManager()findFragmentById(R.id.inner_content2)。
FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
ft.setCustomAnimations(R.anim.slide_in_right,R.anim.slide_out_left,R.anim.slide_in_left,R.anim.slide_out_right);
如果(!frag.getTag()。等于(dealsinfo))
{
捆绑ARGS =新包();
args.putString(dealid,bean.getId());
mdealinfo.setArguments(参数);
ft.replace(R.id.inner_content2,mdealinfodealsinfo);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(NULL);
ft.commit();
}
}
}
}
当在片段执行交易, onCreateView()
和 onActvityCreated()
将会再次调用,但它不会被称为的onCreate()
方法。因此,保持的onCreate()数据
方法。如果数据已经加载,直接设置使用一些额外的标志适配器以及加载web服务的数据管理。
有关片段生命周期的详细信息,请参阅此
I have showing list of items which is displayed in grid and the data is coming from webservice . And when i click on the item it takes to the new screen but when coming from this screen to the screen having Grid the layout is recreated but i want to save the instance like activity so that the screen is not created again. what i can do to achieve this ?
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mfragmentManager = getActivity().getSupportFragmentManager();
mhandler=new Handler(this);
mLvAllDeals = (GridView) mview.findViewById(R.id.xLvAllDeals);
mLvAllDeals.setCacheColorHint(0);
mLvAllDeals.setOnItemClickListener(this);
new MyDealsAsyncTask().execute();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAlDealName = new ArrayList<AllDealsData>();
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if(arg0==mLvAllDeals)
{
position = arg2;
AllDealsData bean;
bean = mAlDealName.get(position);
Fragment frag = getFragmentManager().findFragmentById(R.id.inner_content2);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
if (!frag.getTag().equals("dealsinfo"))
{
Bundle args = new Bundle();
args.putString("dealid", bean.getId());
mdealinfo.setArguments(args);
ft.replace(R.id.inner_content2, mdealinfo, "dealsinfo");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
}
}
When performing transactions in fragments, onCreateView()
and onActvityCreated()
will be called again but it wont be called onCreate()
method. So maintain data in onCreate()
method. If data is already loaded, set directly to adapter as well as loading webservices data manage it using some extra flag.
For more information on fragment life cycle refer this link
这篇关于在片段屏幕不断加载一遍又一遍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!