0

In my newest android app I'm using Loaders to load data from the internet. I have a RecyclerView and when I click on an item a new Activity is started. But every time I navigate back the Loader reloads the data and therefore updates the RecyclerView which leads to a jump to the top of the list.

How can I stop that behaviour?

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(savedInstanceState == null) {
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        pagerAdapter = new PagerAdapter(getApplicationContext(), getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(3);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }
}

ListFragment

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    getLoaderManager().initLoader(0, null, this);
}

@Override
public Loader onCreateLoader(int id, Bundle args) {
    return new DataLoader(getActivity());
}

@Override
public void onLoadFinished(Loader<Object> loader, Object data) {
    adapter.data = data;
    adapter.notifyDataSetChanged();
}

Code to start new Activity

Intent intent = new Intent(view.getContext(), DetailActivity.class);
intent.putExtra(DetailFragment.ARG_DATA, data);
view.getContext().startActivity(intent);

Back navigation

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}

EDIT: Added the RecyclerView Code

public class ListItemRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public List<Data> data;

    public ListItemRecyclerViewAdapter(List<Data> data) {
        data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ListItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         ((ListItemViewHolder) holder).setData(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
iONsky
  • 21
  • 1
  • 4

2 Answers2

0

Have the same problem ... seems like the Loader calls onLoadFinished in onResume. To fix just call destroyLoader in onLoadFinished.

More details here: Why is onLoadFinished called again after fragment resumed?

0

override the deliverResult and cached the data of previous loader and store that now onStartLoading check if stored variable contains data or not if not forceload else use the cached data this way it will prevent the data to reload im new to android btw