M
M
MamaLuyba2021-11-13 17:10:23
Android
MamaLuyba, 2021-11-13 17:10:23

How to properly organize navigation in Android through MVVM?

Good afternoon.

I started to study android development and faced a seemingly simple, but, nevertheless, nowhere really described task: organization of navigation through the application.

What's the problem: in login_layout there is a button to go to registration (registration_layout). The button is bound to a PublishSubject in the LoginViewModel. PublishSubject was chosen because an unpleasant effect occurs through the standard implementation of MutableLiveData: when the LoginActivity is reopened, observe this LiveData, which leads to a redirect to the registration page, which, in turn, triggers the same observe, but already to the login page - in total, endless cycle. I did not find a solution to this problem: even in books a la: "Android for serious boys", they give examples of some left-handed applications, such as the BeatBox panel, in which, obviously, there is no navigation.

PublishSubject solved the problem of an endless redirect (because it does not emit the last value when subscribing), but there is one more problem: it is not clear how the "back" button (physical BackButton) works.

What I mean: there are two pages - login and registration. From both of them you can go back and forth. Logically, all transitions should be stored in the history stack: login - registration - login - registration, etc. And when you click on the button, the previous page should open. But what happens now: the transition is random - sometimes it works in order, but more often - when you click, the same page opens. Those. On registration I press BackButton - registration opens again. Once again I press - only then the login opens. I click on the BackButton login - the login opens again. And so on, depending on the number of transitions in the chain.

Tried calling finish() after startActivity() - didn't help. Specified noHistory="true" in the manifest - did not help. Did intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - without the same result.

I know that you can somehow organize navigation in a special file, but so far I decided not to touch it. And I do not think that this approach will solve the problem with the BackButton.

Where can I read a good manual on this topic? What books, etc. I did not find such a problem on the net.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Vasilenko, 2021-11-18
@gigacoder

The comments have already said that it is best to delve into JetPack and do navigation in the way recommended by Google.
However, I can offer a transitional option: https://github.com/terrakok/Cicerone
The library is not difficult to use and has enough examples and documentation. In addition, it is simple - you can finish it for yourself. The author of the library is very responsive and considers any suggestions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question