E
E
Evgeny Petrov2019-08-23 15:15:02
Java
Evgeny Petrov, 2019-08-23 15:15:02

Which activity to launch first when developing an application with authorization?

Good afternoon everyone.
I am developing an application with authorization. And in my opinion, my method is like a crutch and the wrong approach, so I would like to hear the advice of experienced people.
At the moment, the algorithm of the program is as follows: the main activity is MainActivity. When the application starts, MainActivity is launched and checks if there is a token in the SharedPref, if there is a token, buzz, we continue to work in MainActivity, if there is no token, then we launch LoginActivity to authorize the user. If the user logged in successfully, then restart MainActivity. Since the main activity is launched several times, of course, the life cycle methods are triggered several times, which is very inconvenient.
Question: how to organize it correctly? What activity to make the main one? Or are there some mechanisms that can check for the presence of a token before launching an activity? Google doesn't seem to be able to ask the right question. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petrov, 2019-08-24
@kazsat

In general, I found a solution myself:
Create an activity with Theme.NoDisplay theme and make it a launcher. In this activity we write the code:

Intent intent;

        if (!isExistToken()) {
            intent = new Intent(this, LoginActivity.class);
        } else {
            intent = new Intent(this, MainActivity.class);
        }

        startActivity(intent);
        finish();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question