Tarik Ziyad
Tarik Ziyad

Reputation: 115

the first fragment in tabbed activity is always blank

I'm using tabbed activity to show three tabs the problem is, the first tab is always blank, while the second and third works. and if i click the third one and after that click on the first one, the first loads the data

public class Main2Activity extends AppCompatActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public static class SectionsPagerAdapter extends FragmentStatePagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a Movies_Fragment (defined as a static inner class below).
        Movies_Fragment fragment =  Movies_Fragment.newInstance(position );
        return fragment;

    }



    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }




    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Most Popular";
            case 1:
                return "Top Rated";
            case 2:
                return "Top Rated 2";
        }
        return null;
    }
}

}

public class Movies_Fragment extends Fragment {

public static ImageAdapter imageAdapter;


public static Movies_Fragment newInstance(int sectionNumber) {
    Movies_Fragment fragment = new Movies_Fragment();
    Bundle args = new Bundle();
    args.putInt("index", sectionNumber);
    fragment.setArguments(args);

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);


}

@Override
public void onStart() {
    super.onStart();
    updateMovies();
    //imageAdapter.notifyDataSetChanged();

}

public void updateMovies() {
    FetchMovieTask movieTask = new FetchMovieTask();
    Bundle bundle = getArguments();
    int index = bundle.getInt("index");
    movieTask.execute(index);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_movies_, container, false);

    imageAdapter = new ImageAdapter(getActivity());
    GridView gridView = (GridView)rootView.findViewById(R.id.gridView);
    gridView.setAdapter(imageAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            MDMovie movie = (MDMovie) imageAdapter.getItem(i);
            Intent intent = new Intent(getActivity(),movieDetailsActivity.class);
            intent.putExtra("movie", movie);
            startActivity(intent);
        }
    });

    return  rootView;
}

}

Upvotes: 3

Views: 678

Answers (1)

Shaishav
Shaishav

Reputation: 5312

This happened to one of my projects also and I was unable to diagnose it then too. I ended up using this [dirty] hack. Its not a solution by far but, it worked for us. We basically simulate a page change on start:

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

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

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

    mViewPager.setCurrentItem(1);
    mViewPager.setCurrentItem(0);
} 

Upvotes: 1

Related Questions