Android: onCreate not working in FragmentActivity when its corresponding XML is bloated?

New to Android development, and I'm trying to run an instance of a Google Maps action inside a tab.

I am currently set up using ViewPager / TabLayout, using a custom fragment pager class that stores a fragment for each tab, with each corresponding fragment inflating the XML file for the Activity that I want them to contain in their onCreateView method. The code is as follows:

// Inside main activity onCreate
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MainFragmentPager(getSupportFragmentManager(), MainActivity.this));

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

..

// Fragment pager class
...
private Fragment pageFragments[] = new Fragment[] {
        MapsPageFragment.newInstance(),
        DetailsPageFragment.newInstance()
};

@Override
public Fragment getItem(int position) {
    return pageFragments[position];
}
...

and

// Inside each fragment class
...
public static MapsPageFragment newInstance() {
    MapsPageFragment fragment = new MapsPageFragment();
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.activity_maps, container, false);

    return view;
}
...

with Activity_maps being Google Maps pre-generated XML activity:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.<name>.MapsActivity" />

and MapsActivity, the associated FragmentActivity function.

The problem, which, it seems to me, is as follows:

  • , Activity_maps, , XML (, ), .

  • , , onCreate MapsActivity, .

, ? , FragmentActivity Google Maps , , , TabLayout/ViewPager ( ).

+4

Source: https://habr.com/ru/post/1661257/


All Articles