I have a problem, which I consider easy, but due to my little experience in Android I can not solve.
You see, I'm developing an app that gets the data from a server, that is, I make requests to get the data to be displayed Ejm: list of products.
I have a main activity, which calls fragments by a viewpager, everything works well until I want to see a product, that is, when I tap on one and send me to the other activity and back to the activity again main, it seems that the content is duplicated or the request is made again and it adds again the same elements
This is the MainActivity code
public class MainActivity extends AppCompatActivity {
ViewPager pager;
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager= (ViewPager) findViewById(R.id.main_pager);
tabLayout= (TabLayout) findViewById(R.id.tab_layout);
setupViewPager();
}
private void setupViewPager() {
pager.setAdapter(new PagerAdapter(getSupportFragmentManager(), buildFragments()));
tabLayout.setupWithViewPager(pager);
tabLayout.getTabAt(0).setIcon(R.drawable.ic_posts);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_action_message);
}
private ArrayList<Fragment> buildFragments() {
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(new PostsFragment());
fragments.add(new MensajesFragment());
return fragments;
}
}
the requests are made in the fragment, first to the requests in the state onResume () but I change it to the state onStart () and I still have the same problem.
This is the code of the Fragment
public class PostsFragment extends Fragment {
private RecyclerView recycler;
private PostsAdapter adapter;
public static PostsFragment newInstance() {
PostsFragment fragment = new PostsFragment();
return fragment;
}
public PostsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new PostsAdapter(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_posts, container, false);
// Obtener el Recycler
recycler = (RecyclerView) root.findViewById(R.id.posts_recycler);
recycler.setHasFixedSize(true);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recycler.setLayoutManager(linearLayoutManager);
recycler.setAdapter(adapter);
return root;
}
@Override
public void onStart() {
super.onStart();
Call<PostsResponse> c = ApiAdapter.getApiService().getPosts(ApiCons.API_KEY,"","DESC");
c.enqueue(new Callback<PostsResponse>() {
@Override
public void onResponse(Call<PostsResponse> call, Response<PostsResponse> response) {
adapter.agregarTodo(response.body().getPosts());
}
@Override
public void onFailure(Call<PostsResponse> call, Throwable t) {
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onResume() {
super.onResume();
}
could help me with your experience to solve this problem. Thanks.