In the belowcode, we have a TrendsFragment and a ProfileFragment as base fragments that will be opened with bottom navigation view. I can open a Blank Fragment and navigate to it from both of these. When I do TrendsFragment-profileFragment-BlankFragment-TrendsFRagment, BlankFragment gets destroyed. I tried a lot of ways to change it but I could not manage it. How can I make blankFragment live? I started to use navigation component library because the documantation says :
"The NavigationUI class includes APIs that automatically save and restore the state of menu items as the user moves between them. These APIs implement multiple back stack support by default in the following cases:
When you use the appropriate overload of setupWithNavController() to associate an instance of NavigationView or BottomNavigationView with a NavController instance, as described in Add a navigation drawer or Bottom navigation. When you use onNavDestinationSelected() to create a custom navigation menu UI tied to destinations hosted by a NavController instance."
What am I doing wrong? please explain clearly, thanks in advance...
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
ActivityMainBinding binding;
NavController navController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
NavHostFragment navHostFragment=(NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_container);
navController = navHostFragment.getNavController();
NavigationUI.setupWithNavController(binding.forumBottomNav, navController);
}
}
Here is my navigation graph
<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/trendsFragment">
<fragment
android:id="@+id/profileFragment"
android:name="com.yusufemirbektas.bottomnavfrags.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" >
<action
app:restoreState="true"
android:id="@+id/action_profileFragment_to_blankFragment"
app:destination="@id/blankFragment" />
</fragment>
<fragment
android:id="@+id/trendsFragment"
android:name="com.yusufemirbektas.bottomnavfrags.TrendsFragment"
android:label="fragment_trends"
tools:layout="@layout/fragment_trends" >
<action
app:restoreState="true"
android:id="@+id/action_trendsFragment_to_blankFragment"
app:destination="@id/blankFragment" />
</fragment>
<fragment
android:id="@+id/blankFragment"
android:name="com.yusufemirbektas.bottomnavfrags.BlankFragment"
android:label="fragment_blank"
tools:layout="@layout/fragment_blank" >
<action
app:restoreState="true"
android:id="@+id/action_blankFragment_self"
app:destination="@id/blankFragment" />
</fragment>
Trends fragment
public class TrendsFragment extends Fragment {
FragmentsListener listener;
FragmentTrendsBinding binding;
FragmentManager fm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fm = getChildFragmentManager();
}
public void setListener(FragmentsListener listener) {
this.listener = listener;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentTrendsBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NavController navController= Navigation.findNavController(view);
binding.openFragButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navController.navigate(R.id.action_trendsFragment_to_blankFragment);
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onDestroy() {
super.onDestroy();
fm = null;
listener = null;
}
}
profile fragment
public class ProfileFragment extends Fragment {
private static final String TAG = "ProfileFragment";
FragmentProfileBinding binding;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding=FragmentProfileBinding.inflate(inflater,container,false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NavController navController= Navigation.findNavController(view);
binding.openFragButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navController.navigate(R.id.action_profileFragment_to_blankFragment);
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding=null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
blank fragment
public class BlankFragment extends Fragment {
FragmentBlankBinding binding;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentBlankBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NavController navController= Navigation.findNavController(view);
binding.openFragButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
navController.navigate(R.id.action_blankFragment_self);
}
});
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
You need to make each nav graph destination a top level destination. You can do so by passing each nav destination id as a set to appbarconfiguration