15

I know, there are many forum posts on this topic already, but none did solve my issue

My code looks like this:

private SectionsPagerAdapter myAdapt;
private ViewPager myPager;
....

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

    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.
    if (mSectionsPagerAdapter == null) {
        mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
    }
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
....

But I get the Cannot resolve method getChildFragmentManager () which seems to make many problems.

I already tried a pretty whole lot of things:

  • I checked the inbound support library for the correct version.
  • I tried to reference the method as android.support.v7.app.getChildFragmentManager() which got me the Cannot make static reference from non-static context error.
  • I reimported the v4 support library

The whole issue arose because the Fragments turn empty whenever I return to the activity. If you have another idea, feel free to answer or comment it, then the whole issue regarding the getChildFragmentManager is solved anyway

I have no clue what to do...

ps: here is the whole class implementation:

import android.support.v4.app.FragmentManager;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class HomeScreen extends AppCompatActivity {

    int index = 0;
    int semIndex = 0;

    public static final String GROUP_EXTRA = "Group";
    public static final String HOME_SCREEN_LOG = "HomeScreen Activity";

    /**
     * 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;

    LayoutInflater inflater;

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

        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.
        if (mSectionsPagerAdapter == null) {
            mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
        }
        // 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);

        final Bundle b;
        if ((b =this.getIntent().getExtras()) != null) {
            AsyncTask at = new AsyncTask() {
                @Override
                protected Object doInBackground(Object[] params) {

                    String  groupName = b.getString(HomeScreen.GROUP_EXTRA),
                            desc = b.getString(NewEditGroupActivity.DESC_EXTRA),
                            time = b.getString(NewEditGroupActivity.TIME_EXTRA),
                            date = b.getString(NewEditGroupActivity.DATE_EXTRA);

                    int numOfPart = b.getInt(NewEditGroupActivity.NUM_EXTRA);

                    boolean regular = b.getBoolean(NewEditGroupActivity.IS_NEW);

                    ExpandListChild elc = new ExpandListChild(desc,String.valueOf(numOfPart),date,time,regular);
                    ExpandListGroup elg = new ExpandListGroup(groupName);

                    MyLgFragment lf = (MyLgFragment) mSectionsPagerAdapter.getItem(0);
                    AllLgFragment af = (AllLgFragment) mSectionsPagerAdapter.getItem(1);

                    af.add(elc,elg);
                    lf.add(elc,elg);
                    return null;
                }
            };
        }

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);    
        inflater = getLayoutInflater();
    }


    @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_home_screen, 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);
    }
}

Let me know if you need further information.

Dominik Reinert
  • 1,075
  • 3
  • 12
  • 26
  • 1
    Use `getChildFragmentManager ` method for placing and managing Fragments inside of current Fragment and use `getSupportFragmentManager ` method when placing and managing Fragments in Activity – ρяσѕρєя K Jan 29 '16 at 18:00

5 Answers5

46

getChildFragmentManager() is a method of a Fragment. Since you are subclassing AppCompatActivity, you can't access it. If you are using the Fragment from the support library you can use getSupportFragmentManager()

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 6
    okay, thank you. I was using the supportFragmentManager, but the fragments turn empty whenever I change the activity and return to it again. A suggestion on stackoverflow was to use the ChildManager. how can I "solve the emptyness problem" ? – Dominik Reinert Jan 29 '16 at 17:57
  • What you mean by emptyness? Could you post the adapter? – Blackbelt Jan 29 '16 at 17:59
  • Whenever I switch to another Activity and come back, all the entries that were in the SectionsPagerAdapter are gone... See this google search: https://www.google.de/search?q=android+sectionspageradapter+empty&ie=utf-8&oe=utf-8&gws_rd=cr&ei=cK2rVobfEsHWO6CHo6gF – Dominik Reinert Jan 29 '16 at 18:20
  • 4
    They are all describing the same issue while fixing it by replacing `getSupprtFragmentManager` by `getChildFragmentManager` – Dominik Reinert Jan 29 '16 at 18:21
  • `SectionsPagerAdapter` is not a class of `Android`. Did you write it ? May I see the code? What are `getCount` and `getItem` returning? – Blackbelt Jan 29 '16 at 18:23
  • Oh sorry, right. Didn't even notice anymore. There you go: [pastebin.com](http://pastebin.com/FPd7KPwx) – Dominik Reinert Jan 29 '16 at 18:30
  • (It's three classes) – Dominik Reinert Jan 29 '16 at 18:30
  • the emptiness is because the Adapters of the List of your Fragments are always empty. The `AsyncTask` is supposed to fill those up, but never execute it (it is only instantiated). – Blackbelt Jan 29 '16 at 18:47
  • Oh, you are right, but if I filll the list with dummy entries, these entries disappear whenever I return to this activity – Dominik Reinert Jan 29 '16 at 18:53
  • I have the whole project in a repository, do you want to test it? – Dominik Reinert Jan 29 '16 at 18:59
  • @DoRe, did you figure out the solution for this? I am having the same issue currently. – Sammys Jun 27 '16 at 17:52
  • @Sammys no, sry. The only thing I figured out is: Android is bullshit. Better use codenameOne. – Dominik Reinert Jun 28 '16 at 08:10
5

you should do like:

adapter = new ViewPagerAdapter(this.getChildFragmentManager());
4b0
  • 21,981
  • 30
  • 95
  • 142
Pawan Rathi
  • 51
  • 1
  • 1
4

I had exactly same issue and I managed to resolve it by simply adding:

viewpager.setOffscreenPageLimit(2);

As was suggested in this post.

Alejandro Montilla
  • 2,626
  • 3
  • 31
  • 35
mehti
  • 41
  • 1
3

This is probably not the most elegant solution, but in my case I needed the viewpager to be running in its own fragment, while having the toolbar and tablayout reside in the MainAcitivy. Only drawback is that you have to hide the TabLayout manually in other Fragments which not use the Tab/Viewpager.

But this way you can still call getChildFragmentManager from the Fragment.

@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_rides_host, container, false);

    viewPager = (ViewPager) v.findViewById(R.id.fm_feed_host_viewpager);

    setupViewPager(viewPager);

    ((MainActivity)getActivity()).mTabLayout.setupWithViewPager(viewPager);

    return v;
}

private void setupViewPager(ViewPager viewPager) {

    ViewPagerAdapter adapter = new ViewPagerAdapter(this.getChildFragmentManager());

    adapter.addFragment(new YourFragment(), "Title");
    adapter.addFragment(...);
    viewPager.setAdapter(adapter);
}
Henk-Martijn
  • 2,024
  • 21
  • 25
1

Using Kotlin, used with my Frgament I used:

//using this.childFragmentManager
//not as this.childFragmentManager()
val sectionsPagerAdapter = CameraTabLayoutAdapter(this.childFragmentManager)
BENN1TH
  • 2,003
  • 2
  • 31
  • 42