Start an app with a screen that is not Main?

0
Hello, I would like to know 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 a confirmation to continue. If so, how?

    
asked by anonymous 19.01.2018 / 20:08

2 answers

0

To make this change you need 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 <intent-filter>...</intent-filter> from the .MainActivity node.

Only one activity has this instruction.

    
19.01.2018 / 20:26
0

As I explained earlier in this response, here is recommended. that you use SplashScreen to do so.

I'll try to explain to you, in a simple way, how this structure should work, especially on a system with Login and Dashboard .

Activity SplashActivity will only be responsible for checking if a user is logged in, and you can do this with SharedPreferences .

If a user is logged in, Activity will open the Dashboard screen, otherwise the Login screen.

You can also save tokens from the user to remember who is or even the ID of it, if it is a system more simple.

  

SplashActivity

This screen does not, and should not, have any layout to not make it heavier. We'll just put more essential things, which are 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 you create, and you can put it in the res\drawable folder or it can be the icon of your application that stays in res\mipmap .

The code below should be in 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>

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

The code below is the default logic of your Splash, it will be responsible for performing 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();
    }
}

So, to make this all work normally, you should only define the SplashActivity as a Launcher screen in 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 most recommended because you will not make the user wait for him to access your app. I mean, some people use Timers in SplashActivity and make the user wait 2 or 5 seconds to show the app. The app only checks what you've been told and opens. Very simple and fast pro user.

    
19.01.2018 / 21:49