[英]Android - Blank fragment when returning from backstack
I have a view that shows a List of Properties, at the bottom of the screen there's a button that opens a fragment containing a MapView. 我有一个显示属性列表的视图,在屏幕底部有一个按钮,用于打开包含MapView的片段。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.find_property_btn_map:
PropertyMapFragment fragment = PropertyMapFragment.newInstance(false, null);
getActivity().getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(((ViewGroup) getView().getParent()).getId(), fragment, PropertyMapFragment.class.getSimpleName())
.commit();
break;
}
}
The onCreateView method for my Properties fragments is as follows 我的“属性”片段的onCreateView方法如下
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_properties_list, container, false);
getmBtnMap().setOnClickListener(this);
getmBtnSaveSearch().setOnClickListener(this);
getmBtnSort().setOnClickListener(this);
getmListView().setAdapter(getPropertyAdapter());
getmListView().setOnItemClickListener(this);
getmListView().setOnScrollListener(this);
getmListView().setScrollingCacheEnabled(false);
searchProperties();
return mRootView;
}
searchProperties();
takes care of calling a Web Service and filling the ArrayAdapter
. 负责调用Web服务并填充ArrayAdapter
。
The thing is that, when I open the MapFragment
and then press the back button, my Property fragment
is blank and the buttons do not respond to onClick
events. 事实是,当我打开MapFragment
然后按返回按钮时,我的Property fragment
为空白,并且按钮不响应onClick
事件。
I tried debugging and saw that onCreateView()
is being called when coming back to Property fragment
, but the buttons are no longer working and the listview
is nowhere to be seen. 我尝试调试,发现返回到Property fragment
时会调用onCreateView()
,但是按钮不再起作用,并且listview
也消失了。
What am I doing wrong? 我究竟做错了什么?
If you are trying to launch that web view from a fragment, then try to use add fragment instead of replace . 如果要从片段启动该Web视图,请尝试使用添加片段而不是replace 。
For example : 例如 :
getActivity().getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(YOUR_CONTAINER_ID, fragment, PropertyMapFragment.class.getSimpleName())
.commit();
The code above is replace that fragment container with the new one and not adding that fragment on top of that. 上面的代码是用新的片段容器替换该片段容器,而不是在其顶部添加该片段。 So when you do a replace fragment with that ID , it just replaces that view with new one. 因此,当您使用该ID进行替换片段时,它将只是用新视图替换该视图。
Now, 现在,
Same code but with add: 相同的代码,但添加:
getActivity().getSupportFragmentManager().beginTransaction()
.addToBackStack(null)
.add(YOUR_CONTAINER_ID, fragment, PropertyMapFragment.class.getSimpleName())
.commit();
Now, that web view fragment that you have will be added to the view/fragment and now when you press back from that web view, you previous fragment will be visible. 现在,您拥有的Web视图片段将被添加到视图/片段中,现在当您从该Web视图中按回时,您先前的片段将可见。
Just make sure that that the ID you are replacing is the same as the one in which you have all the other properties. 只要确保您要替换的ID与您拥有所有其他属性的ID相同即可。
Maybe i misinterpreted the question so please correct me in that context. 也许我误解了这个问题,所以请在这种情况下纠正我。
Hope this helps. 希望这可以帮助。
addToBackStack() <--dont include this for your first fragment.--> addToBackStack()<-不要在第一个片段中包含它。-->
if(getSupportFragmentManager().getBackStackEntryCount() !=1){
fragmentTransaction.addToBackStack(null);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.