问题
In my FirstFragment's onCreateView()
method I have the following elements:
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle bundle) {
initSearchView();
String selectedItem = search.getText().toString();
Log.d(TAG, "onCreateView " + selectedItem); //Always empty
}
As you can see, I call initSearchView()
method which looks like this:
private void initSearchView() {
SearchView searchView = rootView.findViewById(R.id.search);
search = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
search.setOnItemClickListener((adapterView, view, position, id) -> {
Item Item = (Item) adapterView.getItemAtPosition(position);
search.setText(item.getItemName()); //Set item name
goToItemFragment(item.getItemName());
});
}
As you can see, once an item is selected, I set the name of that item to the search
object. This works fine since I see that the name of the item is placed inside the search.
Here is the method that I use to go to the SecondFragment:
private void goToISecondFragment(String itemName) {
ActionFirstFragmentToSecondFragment action = actionFirstFragmentToSecondFragment();
action.setItemName(itemName);
Navigation.findNavController(rootView).navigate(action);
}
The problem arrives when I get back from SecondFragment to FirstFragment. Even if I see the selected item name in the search, when the fragment starts, that log statement is always empty, meaning the it doesn't see that text in the search.
How can I get that visible text from search when I get back from SecondFragment to FirstFragment? Thanks
来源:https://stackoverflow.com/questions/58730536/unable-to-get-the-text-from-searchview-searchautocomplete-oncreateview-in-fragme