简体   繁体   English

Android Studio 启动画面在活动开始前消失

[英]Android Studio Splash screen disappearing before activity start

I have problem with splash screen.我有闪屏的问题。 It shows only for half a second and disappears a few seconds before activity HomeZastepowy appears.它只显示半秒钟,然后在 HomeZastepowy 活动出现前几秒钟消失。 I made this splash screen based on this site https://android.jlelse.eu/right-way-to-create-splash-screen-on-android-e7f1709ba154 .我根据这个网站https://android.jlelse.eu/right-way-to-create-splash-screen-on-android-e7f1709ba154制作了这个闪屏。 Where i made a mistake?我在哪里做错了?

MainActivity code:主要活动代码:


import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.Objects;


public class MainActivity extends AppCompatActivity {
     FirebaseAuth mAuth;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAuth = FirebaseAuth.getInstance();

        if (mAuth.getCurrentUser() != null) {
            String uid = mAuth.getUid();
            final DocumentReference mDocRef = FirebaseFirestore.getInstance().collection("Data").document("userInfo").collection(Objects.requireNonNull(uid)).document(uid);
            mDocRef.get()
                    .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                        @Override
                        public void onSuccess(DocumentSnapshot documentSnapshot) {
                            if (documentSnapshot.exists()) {
                                String user = documentSnapshot.getString("user");
                                assert user != null;
                                if (user.equals("user")) {

                                    Log.d("tag", String.valueOf(mAuth.getUid()));

                                    Intent intent = new Intent(getApplicationContext(),
                                            HomeUsrActivity.class);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    Log.d("tag", "Zastepowy");

                                    Log.d("tag", String.valueOf(mAuth.getUid()));

                                    startActivity(new Intent(MainActivity.this, HomeZastepowy.class));

                                    finish();

                                }
                            }
                        }


                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                }
            });
            finish();


        }else {
            Intent intent = new Intent(getApplicationContext(),
                    HomeActivity.class);
            startActivity(intent);
            finish();
        }


        }


    }

AndroidManifest code: AndroidManifest 代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zapp4">

    <application
        android:allowBackup="true"
        android:fullBackupContent="@xml/backup_descriptor"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".HomeUsrActivity"></activity>
        <activity android:name=".LoginActivity" />
        <activity android:name=".punktyog" />
        <activity android:name=".punktywz" />
        <activity android:name=".HomeZastepowy" />
        <activity android:name=".tradycje" />
        <activity android:name=".wiedza" />
        <activity android:name=".kontakt" />
        <activity android:name=".zastepy" />
        <activity android:name=".historia" />
        <activity android:name=".onas" />
        <activity android:name=".HomeActivity" />
        <activity android:name=".MainActivity" android:theme="@style/SplashScreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

I think the problem is that you forgot to remove this line from your code:我认为问题在于您忘记从代码中删除此行:

setContentView(R.layout.activity_main)

Because you setContentView, it overrides your theme background.因为你 setContentView,它会覆盖你的主题背景。 So try to comment this line and give it a try.因此,请尝试评论此行并尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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