The problem
I'm developing an Android app using Kotlin synthetic binding, RxJava, Retrofit and the MVP pattern. On this app there's a screen that's supposed to fetch some data from an API and populate a RecyclerView
with the returned data.
All of that is working every single time, until I try rotate the screen... Once the Activity and Fragment are recreated and try to access the RecyclerView the app crashes with the following exception:
10-25 18:20:00.368 4580-4580/com.sample.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sample.app, PID: 4580
java.lang.IllegalStateException: recyclerView must not be null
at com.sample.app.features.atendimento.ClientesAtendimentosFragment.onAtendimentosLoaded(ClientesAtendimentosFragment.kt:56)
at com.sample.app.features.atendimento.ClienteAtendimentoActivity.showClientesAtendimento(ClienteAtendimentoActivity.kt:126)
at com.sample.app.features.atendimento.ClienteAtendimentoPresenter$getClientesAtendimento$3.accept(ClienteAtendimentoPresenter.kt:38)
at com.sample.app.features.atendimento.ClienteAtendimentoPresenter$getClientesAtendimento$3.accept(ClienteAtendimentoPresenter.kt:18)
at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:63)
at io.reactivex.internal.operators.single.SingleDoAfterTerminate$DoAfterTerminateObserver.onSuccess(SingleDoAfterTerminate.java:71)
This happens when I try to call this method:
override fun onAtendimentosLoaded(atendimentos: List<UsuarioAtendimento>) {
recyclerView.visibility = View.VISIBLE
recyclerView.adapter = ClienteAtendimentoAdapter(atendimentos, this)
}
Since, for some reason, the recyclerView variable is null, the app crashes, even though the Activity and Fragment are both successfully created and are already visible on the phone (I can even see the loading screen before the crash).
So why is my view getting null, even after the Activity and Fragment are already visible?
EDIT: I'm adding the MVP files as well to help understand what's happening here.
Presenter
class ClienteAtendimentoPresenter(private val repository: ClienteAtendimentoRepository): ClienteAtendimentoContract.Presenter {
private var view: ClienteAtendimentoContract.View? = null
private var disposable: Disposable? = null
override fun getClientesAtendimento() {
try {
disposable = repository.getClientesEmAtendimento()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe {
view?.hideErrors()
view?.showLoading()
}
.doAfterTerminate {
view?.hideLoading()
}
.subscribe(
{
view?.showClientesAtendimento(it)
},
{
handleError(it)
}
)
} catch (e: NoConnectionException) {
view?.hideLoading()
handleError(e)
}
}
override fun stop() {
view = null
disposable?.dispose()
}
override fun attachView(view: BaseView<BasePresenter>) {
if (view is ClienteAtendimentoContract.View)
this.view = view
}
}
View
class ClienteAtendimentoActivity : BaseActivity(), ClienteAtendimentoContract.View {
override val presenter: ClienteAtendimentoContract.Presenter by inject()
private lateinit var mSectionsPagerAdapter: SectionsPagerAdapter
inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
private val fragAtendimento = ClientesAtendimentosFragment.newInstance()
private val fragEspera = ClientesAtendimentosFragment.newInstance()
override fun getItem(position: Int): Fragment {
return if (position == 0) fragAtendimento else fragEspera
}
override fun getCount(): Int = 2
}
private fun setupTabs() {
mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)
// Set up the ViewPager with the sections adapter.
container.adapter = mSectionsPagerAdapter
container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_cliente_atendimento)
// ...
setupTabs()
}
override fun onResume() {
super.onResume()
presenter.attachView(this)
presenter.getClientesAtendimento()
}
override fun onPause() {
super.onPause()
presenter.stop()
}
// ...
override fun showClientesAtendimento(atendimentos: AtendimentosLista) {
val atendimentosFrag = mSectionsPagerAdapter.getItem(0) as ClientesAtendimentosFragment
val filaEsperaFrag = mSectionsPagerAdapter.getItem(1) as ClientesAtendimentosFragment
atendimentosFrag.onAtendimentosLoaded(atendimentos.atendimentos) // This is where I'm getting the error.
filaEsperaFrag.onAtendimentosLoaded(atendimentos.espera)
}
// ...
}
Fragment
class ClientesAtendimentosFragment : Fragment(), AtendimentosFragmentCallback, OnClickAtendimentoCallback {
private lateinit var recyclerView: RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_single_rv, container, false)
recyclerView = rootView.findViewById(R.id.recyclerView) // I did this to test if Kotlin synthetic binding was the problem.
return rootView
}
// ...
override fun onAtendimentosLoaded(atendimentos: List<UsuarioAtendimento>) {
recyclerView.visibility = View.VISIBLE // App is crashing here.
recyclerView.adapter = ClienteAtendimentoAdapter(atendimentos, this)
}
// ...
}
What I've tried so far
As suggested on this answer, I tried to make the fragment retain the instance, but that didn't work.
I've also tried to ditch the Kotlin synthetic binding and make the recyclerView variable a lateinit var
that's inflated inside the onCreateView()
method, like the snippet below:
private lateinit var recyclerView: RecyclerView
//...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_single_rv, container, false)
recyclerView = rootView.findViewById(R.id.recyclerView)
return rootView
}
But now I'm getting the following error:
10-25 18:32:42.261 10572-10572/com.sample.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sample.app, PID: 10572
io.reactivex.exceptions.UndeliverableException: kotlin.UninitializedPropertyAccessException: lateinit property recyclerView has not been initialized
at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
at io.reactivex.internal.observers.ConsumerSingleObserver.onSuccess(ConsumerSingleObserver.java:66)
at io.reactivex.internal.operators.single.SingleDoAfterTerminate$DoAfterTerminateObserver.onSuccess(SingleDoAfterTerminate.java:71)
at io.reactivex.internal.operators.single.SingleDoOnSubscribe$DoOnSubscribeSingleObserver.onSuccess(SingleDoOnSubscribe.java:77)
at io.reactivex.internal.operators.single.SingleObserveOn$ObserveOnSingleObserver.run(SingleObserveOn.java:81)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:119)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property recyclerView has not been initialized
at com.sample.app.features.atendimento.ClientesAtendimentosFragment.onAtendimentosLoaded(ClientesAtendimentosFragment.kt:59)
EDIT: I did what Xite suggested on his answer, and I found out that for some reason when I'm rotating the screen, the new Rx call is still trying to use the old Fragment reference, even though new ones have been successfully created and are already displayed. I'm not sure where am I supposed to clean that up, or why is it behaving that way, perhaps it has something to do with the supportFragmentManager
?