Prevent reloading data if android has data?

I have a navigation box with a list of menu items such as updates, photos, etc. Here I am making a transaction fragment based on a click in the navigation box.

 int id = item.getItemId();

 if (id == R.id.nav_updates) {
    UpdatesFragment fragment = new UpdatesFragment();
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.frame_container,fragment);
    fragmentTransaction.commit();
}

Suppose, if I click nav_updates, it will replace frame_container with UpdatesFragment. There is no problem.

  • If I click the same menu item again nav_updateswhen I'm on the same page, it will download the data from the server again and create the view. But I do not want to download again. If we click an item in the navigation box when we are already on the page.

  • When performing paragraph (1), it also overlaps. I think I have a viewpager and 2 tab in UpdatesFragment. If I click an item the first time it loads and shows 2 Tab. When I click again, it overlaps. This means that when I scroll, it gets hit, and I have to make 2 swipe to reach each tab.

  • When it is UpdatesFragmentalready open with data, I want to show old data, if not updated. (I don’t know where I want to put the network code. OnCreate / onCreateView / onActivityCreated)

Thanks in advance!

EDIT 1:

I decided the first release,

UpdatesFragment fragment_t = (UpdatesFragment) getSupportFragmentManager().findFragmentByTag("updates");
            if (fragment_t != null && fragment_t.isVisible()) {
                drawer.closeDrawer(GravityCompat.START);
            }
            else {
                UpdatesFragment fragment = new UpdatesFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frame_container,fragment, "updates");
                fragmentTransaction.commit();
            }
+4
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1625615/


All Articles