Start an app with a screen that isn’t Main?

Asked

Viewed 813 times

0

Hello, I wonder if it is possible to start an application with an Activity that is not the main Activity, and from it call the main, as a kind of login to access the main menu, or even a confirmation to continue. If yes, how?

2 answers

0

As I already explained in my reply here, it is recommended that you use a SplashScreen for such.

I’m going to try to explain to you, in a simple way, how this structure should work, especially in a system with Login and with a Dashboard.

A Activity SplashActivity will only be responsible for checking if there are any logged in users, and you can do this with Sharedpreferences.

If you have a connected user, Activity will open the screen Dashboard, otherwise, the screen of Login.

You can also save tokens user to remember who is connected or even to ID if it is a system simpler.

Splashactivity

This screen has no, and should not, have any layout to not make it heavier. We will only put more essential things, which are the business logic to take the user to another page.

You should set a theme below and use a background on the screen to not make it so ugly. The background can be an image created by you, and you can put it in the folder res\drawable or it can be the icon of your app that is on res\mipmap .

The code below should be res\values\styles.xml:

<style name="AppTheme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@mipmap/ic_launcher</item>
</style>

It will style the screen to not make it so ugly. You can modify it normally. To change the screen icon, modify the attribute windowBackground.

The code below is the standard logic of your Splash, it is she who will be responsible for carrying out user control.

public class SplashActivity extends AppCompatActivity {

    SharedPreferences pref;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        pref = getSharedPreferences("user_info",MODE_PRIVATE);
        isUserLogged = pref.getBoolean("user_logged", false);

        startActivity(new Intent(this, isUserLogged ? DashboardActivity.class : LoginActivity.class));
        finish();
    }
}

And then, to make it all work normally, you just have to define the Splashactivity as screen of Launcher in the Androidmanifest.

<activity
    android:name=".SplashActivity"
    android:theme="@style/AppTheme.Splash">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This type of method is more recommended because you will not make the user wait so he can access your app. I mean, some people use Timers in Splashactivity and make the user wait 2 or 5 seconds to display the app. With this, the app only checks what has been informed and opens. Very simple and faster for the user.

0


To make this change it is necessary to change your manifest.xml

Ex:

<!-- Login -->
<activity
    android:name=".LoginActivity"
    android:theme="@style/AppThemeLogin">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

After doing this, just remove all the code <intent-filter>...</intent-filter> of the knot of .MainActivity.

It only takes one activity possess that instruction.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.