0

My MainActivity uses two types of fragments:

com.google.android.gms.maps.SupportMapFragment

and

com.ankitshubham97.myapp.NotifFragment.

For com.google.android.gms.maps.SupportMapFragment, android.support.v4.app.Fragment is needed but for com.ankitshubham97.myapp.NotifFragment, android.app.Fragment is needed. I cannot import both of them. Is there some neat solution for solving this? I already looked into How to mix android.support.v4.app.Fragment and android.app.Fragment and Android using both getFragmentManager and getSupportFragmentManager causes overlapping but they are a bit old and thought if somebody has a neat solution, apart from what is suggested in the previous links.

Here is my activity.xml's fragment declaration part:

<FrameLayout
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <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">

    </fragment>

    <fragment
        android:id="@+id/notif"
        android:name="com.ankitshubham97.myapp.NotifFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </fragment>
</FrameLayout>

My MainActivity.java's onCreate function(with errors):

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        notifFragment =  getFragmentManager().findFragmentById(R.id.notif);
        getFragmentManager().beginTransaction().hide(notifFragment);
        mapFragment.getMapAsync(this);
        navigation = (AHBottomNavigation) findViewById(R.id.navigation);
        AHBottomNavigationItem item1 = new AHBottomNavigationItem(R.string.title_home, R.drawable.ic_home_black_24dp, R.color.colorBottomNavigationPrimary);
        AHBottomNavigationItem item2 = new AHBottomNavigationItem(R.string.title_dashboard, R.drawable.ic_dashboard_black_24dp, R.color.colorBottomNavigationPrimary);
        AHBottomNavigationItem item3 = new AHBottomNavigationItem(R.string.title_notifications, R.drawable.ic_notifications_black_24dp, R.color.colorBottomNavigationPrimary);
        navigation.addItem(item1);
        navigation.addItem(item2);
        navigation.addItem(item3);
        navigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
            @Override
            public boolean onTabSelected(int position, boolean wasSelected) {
                FragmentManager sfm = getSupportFragmentManager();
                FragmentTransaction sft= sfm.beginTransaction();
                switch (position) {
                    case 0:
                        Log.d(TAG,(mapFragment==null)+"");
                        sft.show(mapFragment);
                        ft.hide(notifFragment);
                        sft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                        sft.commit();
                        Toast.makeText(getApplicationContext(),R.string.title_home,Toast.LENGTH_SHORT).show();
                        return true;

                    case 1:

                        sft.hide(mapFragment);
                        ft.show(notifFragment);
                        sft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                        sft.commit();addToBackStack(null).commit();
                        Toast.makeText(getApplicationContext(),R.string.title_dashboard,Toast.LENGTH_SHORT).show();
                        return true;
                    case 2:
                        Toast.makeText(getApplicationContext(),R.string.title_notifications,Toast.LENGTH_SHORT).show();
                        navigation.setNotification("",2);
                        spEditor.putInt(NOTIFCOUNT,0);
                        spEditor.apply();
                        return true;
                }
                return true;
            }
        });
}
Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61

2 Answers2

0

Where-ever you use "Fragment", use the specific class including the package. So replace "Fragment" with "android.support.v4.app.Fragment" or "android.app.Fragment" depending on the case.

Does that solve your issue?

Frank
  • 12,010
  • 8
  • 61
  • 78
  • Thank you for your reply. Actually the issue was something else(see my answer) and I had already tried your approach beforehand. :) – Ankit Shubham Sep 04 '17 at 20:59
0

I found out the problem. NotifFragment class extended android.support.v4.app.Fragment and that's why I had to use getSupportFragmentManager().findFragmentById(R.id.notif); instead of getFragmentManager().findFragmentById(R.id.notif);. Also in imports, I had to use android.support.v4.app.* instead of android.app.*

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61