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;
}
});
}