gpt4 book ai didi

Android fragment 数据丢失

转载 作者:行者123 更新时间:2023-11-29 21:06:50 27 4
gpt4 key购买 nike

好的,我有一个带有抽屉导航的应用程序。抽屉中的每个菜单项都会打开一个不同的 fragment ,我将其显示给用户并且效果很好。

其中一个 fragment (DashboardClientFragment) 在其 onCreateView() 方法中调用了一个网络服务,然后填充了一个 ViewPager。第一次运行时,它会加载数据并显示得很好。

当我打开抽屉并单击再次打开此 fragment 的同一项目时,问题就开始了。 fragment 加载,我对网络服务的调用再次被调用(它显示来自服务的 json 响应),但是 viewpager 是空的。

下面是问题的一些截图:

screen one - everything fine

screen two - opening the navigation drawer and clicking on the dashboard menu item

screen three - the bug

这是该 Activity 的精简版。

public class DashboardActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);

if (savedInstanceState == null) {
displayView(1, false);
}
}

public void displayView(int id, boolean addBackStack) {
Fragment fragment = null;

switch (id) {
case 1:
fragment = new DashboardClientFragment();
break;
case 2:
fragment = new ClientsFragment();
break;
case 5:
fragment = new StorefrontPreviewFragment();
break;
case 6:
new SessionManager(this).logoutUser();
startActivity(new Intent(this, MainActivity.class));
finish();
break;

default:
break;
}

if (fragment != null) {
drawer.closeDrawers();

FragmentManager fragmentManager = getSupportFragmentManager();
if (addBackStack)
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack(null).commit();
else
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}
}


}

这是我的 fragment (完整代码)

public class DashboardClientFragment extends Fragment implements OnClickListener {

private ViewPager pager;
private TextView add_booking;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dashboard_client, container, false);

((DashboardActivity) getActivity()).setSelectedMenu(1);
((DashboardActivity) getActivity()).setBarTitle("Dashboard");

pager = (ViewPager) v.findViewById(R.id.pager);
add_booking = (TextView) v.findViewById(R.id.add_booking);

add_booking.setOnClickListener(this);

final Map<String, String> params = new HashMap<String, String>();
params.put("auth", DashboardActivity.user.get(SessionManager.AUTH));

Log.d("params", params.toString());

StringRequest dash = new StringRequest(
Method.POST,
G.DOMAIN + "index/rest/dashboard",
new Listener<String>() {

@Override
public void onResponse(String json) {

Log.d("json", json);

DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getActivity().getSupportFragmentManager(), json);
pager.setAdapter(pagerAdapter);
}
},
new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("error", "Error: " + error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() {
return params;
}
};

AppController.getInstance().addToRequestQueue(dash, null);

return v;
}

@Override
public void onClick(View v) {
if (v == add_booking) {
startActivity(new Intent(getActivity(), BookingActivity.class));
}
}

}

最佳答案

你应该使用 getChildFragmentManager()

替换:

DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getActivity().getSupportFragmentManager(),
json);
pager.setAdapter(pagerAdapter);

与:

DashboardPagerAdapter pagerAdapter = new DashboardPagerAdapter(getChildFragmentManager,
json);
pager.setAdapter(pagerAdapter);

如果可行,现在就告诉我 ;)

关于Android fragment 数据丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24281167/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com