英文:
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
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论