У меня есть фрагмент поиска, в котором я выполняю вызов API.Возвращает LiveData.Всякий раз, когда я поворачиваю телефон, всплывающую клавиатуру, я теряю данные, но мои LiveData все еще содержат данные (я проверил, отключив Wi-Fi).Я думаю, что наблюдаю в неправильном месте.Как я могу решить это? MusicSearchListFragment:
class MusicSearchListFragment : ScopedFragment(), KodeinAware, SearchView.OnQueryTextListener {
override val kodein by closestKodein()
private val searchViewModelFactory: MusicSearchListViewModelFactory by instance()
lateinit var progressBar: ProgressBar
private lateinit var viewModel: MusicSearchListViewModel
private var recyclerView: RecyclerView? = null
private val resId = R.anim.layout_animation_waterfall
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.search_fragment, container, false)
viewModel = ViewModelProviders.of(this, searchViewModelFactory).get(MusicSearchListViewModel::class.java)
progressBar = rootView.findViewById(R.id.loading_progressbar_search)
progressBar.visibility = View.GONE
recyclerView = rootView.findViewById(R.id.search_recycler_view) as? RecyclerView
recyclerView!!.layoutManager = LinearLayoutManager(context)
return rootView
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(activity as AppCompatActivity).supportActionBar?.hide()
search_toolbar.apply {
setIconifiedByDefault(false)
requestFocus()
setOnQueryTextListener(this@MusicSearchListFragment)
isIconified = false
requestFocusFromTouch()
}
}
override fun onQueryTextChange(newText: String?): Boolean {
return true
}
override fun onQueryTextSubmit(query: String?): Boolean {
Log.d("myLog", "Make Retrofit Call")
progressBar.visibility = View.VISIBLE
launch(Dispatchers.Main) {
if (query != null) {
val searchedTracks = viewModel.searchTracks(query)
searchedTracks.observe(this@MusicSearchListFragment, Observer {
if (it == null) return@Observer
progressBar.visibility = View.GONE
val adapter = MusicSearchListAdapter(
this@MusicSearchListFragment,
it.message.body.trackList
)
val animation: LayoutAnimationController = AnimationUtils.loadLayoutAnimation(context, resId)
recyclerView!!.layoutAnimation = animation
recyclerView!!.adapter = adapter
})
} else Toast.makeText(context, "No Internet Connection", Toast.LENGTH_SHORT).show()
}
return false
}
}