[英]recyclerview is empty when back pressed from fragment
On Fragment1 i have onClick method, when i click the button, i got the recycleview list of items. 在Fragment1上我有onClick方法,当我点击按钮时,我得到了recycleview项目列表。 When i click the item in recycleview(use getPlaceFromItem method), i go to the Fragment2.
当我单击recycleview中的项目(使用getPlaceFromItem方法)时,我转到Fragment2。 if device is phone then put the Fragment2 in container1, but if device is tablet(landscape) then i put Fragment2 into container2 next to Fragment1 in container1.
如果设备是手机然后将Fragment2放入container1,但如果设备是平板电脑(横向),那么我将Fragment2放入container1中Fragment1旁边的container2。
Now, when i press back button when device is phone, i get empty recycleview. 现在,当我在设备是电话时按下后退按钮,我得到空的recycleview。
In addition i check orientation of the screen and when the device is phone and i am in Fragment2(ie Fragment2 in container1), then when the screen will be landscape i will put into container1 Fragment1, and into container2 at Fragment2. 此外,我检查屏幕的方向,当设备是电话,我在Fragment2(即容器1中的Fragment2),然后当屏幕将是横向时,我将放入container1 Fragment1,并进入Fragment2的container2。 My problem is how can i cut Fragment2 from container1 and put it into container2.
我的问题是如何从container1切割Fragment2并将其放入container2。
Main Activity : 主要活动 :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
View v = findViewById(R.id.container2);
v.setVisibility(View.GONE);
getSupportFragmentManager().beginTransaction()
.add(R.id.container1, new FragmentA())
.commit();
}
@Override
public void getPlaceFromItem(Place place) {
Bundle bundle = new Bundle();
bundle.putDouble("lat", place.getLat());
bundle.putDouble("lng", place.getLng());
Fragment2 fragment2 = new Fragment2();
fragment2.setArguments(bundle);
if(getResources().getBoolean(R.bool.isTab)) {
View v = findViewById(R.id.container2);
v.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container2,fragment2)
.addToBackStack(null)
.commit();
}
else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container1, fragment2)
.addToBackStack(null)
.commit();
}
}
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
View v = findViewById(R.id.container2);
v.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container1, new Fragment1())
// The next row is a problematic!!!
.replace(R.id.container2, new Fragment2())
.commit();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
}
}
Remember when a fragment is popped out from back stack it comes back with no view. 请记住,当一个片段从后面的堆栈中弹出时,它会返回而没有任何视图。 So you have to attach the view again.
所以你必须再次附加视图。
I recommend for you to keep track of the main View in Fragment in order to avoid views from recreating. 我建议您跟踪片段中的主视图,以避免重新创建视图。 This way once you come back to the fragment with the RecyclerView it will be there.
这样一旦你回到使用RecyclerView的片段,它就会在那里。 The code would look like this.
代码看起来像这样。
public class BlankFragment2 extends Fragment {
public View myRoot;
public BlankFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (myRoot == null) {
myRoot = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
return myRoot;
}
} }
.addToBackStack(null) remove it from code .addToBackStack(null)从代码中删除它
i hope it will work for you 我希望它对你有用
Add .addToBackStack(fragment.getClass().getCanonicalName())
or .addToBackStack("anyString")
to save fragment instance. 添加
.addToBackStack(fragment.getClass().getCanonicalName())
或.addToBackStack("anyString")
来保存片段实例。 Also use add
method, not replace
when you add new fragments. 还可以使用
add
方法,而不是在添加新片段时replace
。
when you are adding your first fragment you should not use addToBackStack(null). 当您添加第一个片段时,不应使用addToBackStack(null)。
for first fragment write like this 对于第一个片段写这样的
getSupportFragmentManager().beginTransaction()
.add(R.id.container1, FragmentA)
.commit();
for other fragments you should use 对于你应该使用的其他片段
getSupportFragmentManager().beginTransaction()
.replace(R.id.container1, FragmentB)
.addToBackStack(null)
.commit();
hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.