问题
i want to add fragment B on top of fragment A without view of fragment A getting destroyed. Only option i see in navigation library is to use navigate method to open a fragment but how to ensure fragment B is added on top of fragment A, not replaced.
navigationController.navigate(R.id.B, bundle);
and how to find the instance of fragment A to be able to interact with it?
回答1:
Will like to know so as to clarify myself. Why don't you want fragment A be destroyed when you navigation to fragment B?
Guess if you won't want to loose fragment A's state, you can initialize it in a viewModel and retrieve it in the onCreate of your activity( whichever lifecycle method of your fragment you determines sounds fit). If this wasn't the reason, plz do explain to provide us with a better context.
For the navigation library way of swapping fragments out of the container. here's a brief summary of the navigation library
As per the docs(which I recommend you should read because it's a lengthy procedure with a lot fuzzy words.
Assuming you are using at least Android Studio 3.3 or higher,
1- add navigation library to project.
dependencies {
def nav_version = "2.0.0"
implementation "androidx.navigation:navigation-fragment:$nav_version" // For Kotlin use navigation-fragment-ktx
implementation "androidx.navigation:navigation-ui:$nav_version" // For Kotlin use navigation-ui-ktx
}
2-Create a navigation graph.
To add a navigation graph to your project, do the following:
In the Project window, right-click on the res directory and select New > Android Resource File.
The New Resource File dialog appears. Type a name in the File name field, such as "nav_graph".
Select Navigation from the Resource type drop-down list, and then click OK.
3- Add a navhost to an activity
within the xml view of the parent activity wherein you want the swapping to be taking place. The activities whose view poses the container view in which you wish to be swapping in this case, add the following code snippet with the appropriate navigation graph name.
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/my_navigation_graph_file_name" />
not the attribute defaultNavHost set to true to indicate to the navigation library that it should take over the handling of the back button and up button for proper navigation within the application in an intuitive manner.
6- Add destinations to the graph.
There are three different ways to add destinations to your navigation graph. You can create a destination from an existing fragment( the fragments you are already swapping in the question context) or activity, create a new destination, or create a placeholder to later replace with a fragment or activity.
To add a new destination type using the Navigation Editor, do the following:
In the Navigation Editor, click the New Destination icon , and then click Create new destination.
In the New Android Component dialog that appears, create your fragment. For more information on fragments, see the fragment documentation.
7- Designate a destination as the start destination
Once you have all of your destinations in place, you can choose a start destination by doing the following:
In the Design tab, click on the destination to highlight it.
Right-click on the destination and click Set as Start Destination.
8- Connect destinations
In the Design tab, hover over the right side of the destination that you want users to navigate from. A circle appears over the right side of the destination.
Click and drag your cursor over the destination you want users to navigate to, and release. The resulting line between the two destinations represents an action.
Click on the arrow to highlight the action. The following attributes appear in the Attributes panel:
The Type field contains “Action”. The ID field contains the ID for the action. The Destination field contains the ID for the destination fragment or activity.
Click the Text tab to toggle to the XML view. An action element is now added to the source destination. The action has an ID and a destination attribute that contains the ID of the next destination, as shown in the image following:
9- Navigate to a destination
Navigating to a destination is done using a NavController, an object that manages app navigation within a NavHost. Each NavHost has its own corresponding NavController.
To retrieve the NavController for a fragment, activity, or view, use one of the following methods:
NavHostFragment.findNavController(Fragment)
Navigation.findNavController(Activity, @IdRes int viewId)
Navigation.findNavController(View)
Once you retrieve a NavController, use its navigate() method to navigate to a destination. The navigate() method accepts a resource ID of either an action or a destination.
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.next_fragment_destination_name, null));
and many more wonderful things the navigation library has to offer us android devs. Avoiding to make the answer very lengthy that's why I didn't include everything. Recommend you checkout the official doc for navigation library and android architecture components as a whole. Hope this helps.
来源:https://stackoverflow.com/questions/53803598/how-to-interact-with-other-fragment-when-using-navigation-architecture-component