Here is what I personally did for one of my small mobile projects. Instead of creating a separate Activity free screen where the condition is satisfied and which launches the corresponding screen, I set the condition to one Activity and made a dynamic setContentView() , as in:
if (!userIsLoggedIn) { setContentView(R.layout.signup); } else { setContentView(R.layout.homescreen); }
Two important points to this approach:
1: Instead of writing this to onCreate() , you want to make an exact decision inside onResume() , because the latter is always called whenever the screen should be displayed in front. You can see this on the Android activity life cycle . So, if, for example, a user has just downloaded my application and launched it for the first time, because no user has logged in, it will be sent to the registration page. When she registered and for some reason presses the HOME button (not BACK , which completely deletes the application!), And then resumes the application, the layout that she sees is already the initial screen. If I put a conditional value inside onCreate() , what would be displayed is the registration screen, because according to the life cycle, it does not return to onCreate() when the application returns to the beginning.
2: This solution is ideal only if the merging of the functions of these two Acts does not lead to the creation of a long devilish block of code. As I said, my project was small (its main function is found in the background), so there wasn’t too much for one dynamic Activity . A screen without Activity definitely suitable for making your code more human-readable.
Matt Quiros Aug 03 2018-12-12T00: 00Z
source share