简体   繁体   中英

Android fragment data loss

Ok so i have an app with the navigation drawer. Every menu item in the drawer opens a different fragment which i display to the user and it works great.

One of these fragments (DashboardClientFragment) has a call to a webservice in its onCreateView() method and then populates a ViewPager. At first run it loads the data and it displays just fine.

The problem begins when i open the drawer and i click on the same item that opens this fragment again. The fragment loads, my call to the webservice is called again (it shows the json responce from the service), but the viewpager is empty.

Below are some screenshots of the problem:

screen one - everything fine

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

screen three - the bug

Here is a stripped down version of the 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();
    }
}


}

And this is my fragment (full code)

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));
    }
}

}

You should use the getChildFragmentManager()

replace:

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

with:

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

Let me now if it works ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM