Android:应用程序不会加载我的启动画面,直接转到引导屏幕。

huangapple go评论65阅读模式
英文:

Android: App won't load my splash screen and moves directly to the onboarding screens

问题

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mentalhealthapp">

    <application
        android:allowBackup="true"
        android:icon="@drawable/treen_app_logo"
        android:label="treen"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".HomeActivity" />
        <activity android:name=".SignInActivity" />
        <activity android:name=".SignUpActivity" />
        <activity android:name=".OnboardingActivity">
            <!-- Remove the intent-filter from here -->
        </activity>
        
    </application>

</manifest>
英文:

I have just gotten into Android app development and have been working on parts of my project in bits and pieces.

I first created my onboarding screens and then made my animated splash screen. However, when I run my application my splash screen does not show and instead loads the onboarding pages first. How can I fix this to make my app first transition through the splash screen before moving on the onboarding screens?

This is my SplashActivity Class code

public class SplashActivity extends AppCompatActivity {

    private static int SPLASH_SCREEN = 5000;

    // Variables
    Animation topAnim;

    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        // Animations
        topAnim = AnimationUtils.loadAnimation(this,R.anim.top_animation);

        // Hooks
        image = findViewById(R.id.splashScreenLogo);
        image.setAnimation(topAnim);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, OnboardingActivity.class);
                startActivity(intent);
                finish();
            }
        }, SPLASH_SCREEN);

    }
}

This is my OnboardingActivity Class code

public class OnboardingActivity extends AppCompatActivity {

    private OnboardingAdapter onboardingAdapter;
    private LinearLayout layoutOnboardingIndicators;
    private MaterialButton buttonOnboardingAction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_onboarding);


        // Onboarding Screens
        layoutOnboardingIndicators = findViewById(R.id.layoutOnboardingIndicators);
        buttonOnboardingAction = findViewById(R.id.buttonOnboardingAction);

        setupOnboardingItems();

        final ViewPager2 onboardingViewPager = findViewById(R.id.onboardingViewPager);
        onboardingViewPager.setAdapter(onboardingAdapter);


        setupOnboardingIndicators();
        setCurrentOnboardingIndicator(0);


        onboardingViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                setCurrentOnboardingIndicator(position);
            }
        });

        buttonOnboardingAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onboardingViewPager.getCurrentItem() + 1 < onboardingAdapter.getItemCount()) {
                    onboardingViewPager.setCurrentItem(onboardingViewPager.getCurrentItem() + 1);
                }
                else {
                    startActivity(new Intent(getApplicationContext(), SignUpActivity.class));
                    finish();
                }
            }
        });

    }

}

Edit - My Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mentalhealthapp">

    <application
        android:allowBackup="true"
        android:icon="@drawable/treen_app_logo"
        android:label="treen"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashActivity"></activity>
        <activity android:name=".HomeActivity" />
        <activity android:name=".SignInActivity" />
        <activity android:name=".SignUpActivity" />
        <activity android:name=".OnboardingActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

What should I change in my Manifest File?

答案1

得分: 1

你需要在你的 AndroidManifest.xml 文件中将启动活动设置为 SplashActivity

<activity
    android:name=".SplashActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

You need to set the Launch activity as SplashActivity in your AndroidManifest.xml file

&lt;activity
     android:name=&quot;.SplashActivity&quot;&gt;
     &lt;intent-filter&gt;
        &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

        &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
    &lt;/intent-filter&gt;
&lt;/activity&gt;

huangapple
  • 本文由 发表于 2020年10月3日 04:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64178118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定