简体   繁体   中英

How to start another activity after a splash screen in android studio

Hi everyone I'm studying now and developing a app in android studio. My problem is i can't proceed to my log in activity after splash screen. I already look in the net and stackoverflow and applied it, but still same error. I appreciate for the help and answer.

Here are some of my code

SplashScreen.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            final Intent mainIntent =  new Intent(SplashScreen.this, Login.class);
            SplashScreen.this.startActivity(mainIntent);
            SplashScreen.this.finish();
        }
    },3000);

}

Login.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    Intent intent = new Intent(Login.this, MainActivity.class);
    startActivity(intent);
    finish();
  }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashScreen"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".Login"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".Login"/>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="com.ex.app.MainActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

where did i go wrong ?

As you have written code to move to MainActivity on the method onCreate of Login.java

Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();

Comment this line or keep it on the click of SignIn button

Your login activity just directs you to the MainActivity on create

Look at these lines

Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);

It is redirecting to MainActivity directly as in your LoginActivity onCreate you have written

Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();

without any condition. Make it conditional by putting your login check conditions and criteria

if (<logged_in_your_condition_Check>){
    Intent intent = new Intent(Login.this, MainActivity.class);
    startActivity(intent);
    finish();
}

why you require below lines in Login.class

 Intent intent = new Intent(Login.this, MainActivity.class);
    startActivity(intent);
    finish();

if you are using finish() in onCreate() then it will immediately called onDestroy() and that dosen't make any sense.

remove above given code from onCreate your code will work

in manifest write:

<activity
android:name=".MySplashActivity"
android:noHistory="true"
 ..... >
    ....
</activity>

then in create() or somewhere different cal the method:

 private static int SPLASH_TIME_OUT=2000;

 
private void nextScreen() {
    new Handler().postDelayed(()-> {
            Intent intent = new Intent(MySplashaActivity.this, NextActivity.class);
            startActivity(intent);
            finish();
    }, SPLASH_TIME_OUT);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM