I have a problem while getting data from MainActivity and handle it on Fragment.
At first, there are 4 Fragments. I am using TabLayout
and ViewPager
. Whenever a fragment selected from the Tab, it triggers onPageSelected()
method. Here, I instantiate transaction and send the string URL according to selected Fragment by the means of setArguments()
. In the Fragment I receive string URL with the help of getArguments()
successfully, however when I retrieve data from API using that URL, fetchData()(using Volley)
method ignores it.
Below is onPageSelected() method
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
getSupportFragmentManager().beginTransaction().replace(
R.id.linear_frame,
FragmentAllLanguages.newInstance(0, NetworkUtils.URL_ALL_NEWS))
.commit();
break;
onCreateView() of Fragment
if (getArguments() != null) {
fetchData(getArguments().getString(ARGS_URL));
Toast.makeText(getContext(), "URL is " + getArguments().getString(ARGS_URL), Toast.LENGTH_SHORT).show();
Log.e(TAG, "IF IF IF" + getArguments().getInt(ARGS_ID));
} else {
fetchData(NetworkUtils.URL_ALL_NEWS);
Toast.makeText(getContext(), "else part", Toast.LENGTH_SHORT).show();
Log.e(TAG, "ELSE ELSE ELSE");
}
Finally, "interesting" log file when fragment selected
04-18 02:43:20.764 5610-5610/com.example.orkhan.nexeber E/Volley: ELSE ELSE ELSE
04-18 02:43:20.768 5610-5610/com.example.orkhan.nexeber E/RecyclerView: No adapter attached; skipping layout
04-18 02:43:20.770 5610-5683/com.example.orkhan.nexeber D/EGL_emulation: eglMakeCurrent: 0x9e105420: ver 2 0 (tinfo 0x9e103370)
04-18 02:43:20.794 5610-5610/com.example.orkhan.nexeber E/Volley: IF IF IF2
04-18 02:43:20.850 5610-5610/com.example.orkhan.nexeber E/RecyclerView: No adapter attached; skipping layout
My question is why if statement called twice though it is chosen once in Tab?
My version is maybe because of beginTransaction
's delay, I mean, it cannot send data as fast as oncreateView()
method's call. That's why at first else part is executed and then when if part is executed RecyclerView ignores it.