У меня есть mapsActivity, которая является MainActivity, которая расширяет фрагмент, и я хочу go на другой фрагмент:
ЗДЕСЬ МОЙ XML:
<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=".MainActivity" >
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/top_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
map:labelVisibilityMode="unlabeled"
map:menu="@menu/top_nav"/>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container" />
Вот мой Java код:
public class MainActivity extends AppCompatActivity //FragmentActivity
implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks {
private GoogleMap mMap;
AppBarLayout appBarLayout;
BottomNavigationView bottomNavigation;
Fragment selectedfragment = null;
ImageView settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigation = findViewById (R.id.top_navigation); bottomNavigation.setOnNavigationItemSelectedListener (navigationItemSelectedListener);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener=
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.ic_home:
selectedfragment = new HomeFragment();
editor.apply();
break;
case R.id.ic_chats:
selectedfragment = new ChatsFragment();
break;
case R.id.settings:
selectedfragment = null;
startActivity(new Intent(MainActivity.this, OptionsActivity.class));
break;
}
if (selectedfragment != null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedfragment).commit();
}
return true;
}
};
Намерение работает нормально, только транзакция с двумя фрагментами не будет работать