mskim
mskim

Reputation: 13

why splashactivity Blinking when mainactivity start

enter image description here

It's my project situation now.

  1. MainActivity is LAUNCHER
  2. SplashActivity called MainActivity onCreate()

When I think about it, it looks like there is no problem. but after app starting, The MainActivity screen is briefly visible before SplashActivity call.

Surprisingly, I did not see it on other devices, only galaxy s8.

Of course, I know it is not a general structure. But I can not understand it because I have been working normally. white color is cold start style and splashActivity. red color is mainActivity

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // some getIntent code

    startActivityForResult(new Intent(this, SplashActivity.class), RESULTCODE_);

    setInitLayout();
}

manifest

<activity android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme" >
    <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".SplashActivity"
    android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme" >
</activity>

minSdkVersion 21

targetSdkVersion 28

Upvotes: 0

Views: 2029

Answers (6)

s.j
s.j

Reputation: 651

Use this thread code for splash activity. It will better work :

public class SplashActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 3000;

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

            Thread thread = new Thread() {
                public  void run(){
                    try{
                        sleep(3000);

                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }finally {
                        Intent mainAct=new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(mainAct);
                        finish();
                    }
                }
            };
            thread.start();

        }


        @Override
        protected void onDestroy() {

            super.onDestroy();

        }

    }

Upvotes: 0

Arderun
Arderun

Reputation: 143

If you use style with android:windowBackground for splash activity, don't call setContentView(). Thats all!

Upvotes: 1

s.j
s.j

Reputation: 651

Use this thread code for splash activity it will better work -

public class SplashActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 3000;

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

            Thread thread = new Thread() {
                public  void run(){
                    try{
                        sleep(3000);

                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }finally {
                        Intent mainAct=new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(mainAct);
                        finish();
                    }
                }
            };
        thread.start();

    }


    @Override
    protected void onDestroy() {

        super.onDestroy();

    }

}


and your manifest class will be look like this : 

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

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="AllowBackup,GoogleAppIndexingWarning"> 
      <activity android:name=".Activities.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Upvotes: 0

Black mamba
Black mamba

Reputation: 472

simply add handler for limited seconds of time and finish current activity

Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //actvity transaction
            }
        },3000);

Upvotes: 0

Vinyl
Vinyl

Reputation: 333

You have placed MainActivity class to be the launcher activity in your manifest. I am assuming that your splashscreen activity is called SplashActivity. If you want it to be shown before as the SplashScreen and then MainActivity to come later on, change your manifest code to be:

<activity android:name=".SplashActivity"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme" >
    <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity android:name=".MainActivity"
    android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme" >
</activity>

<activity android:name=".IntroActivity"
    android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme" >
</activity>

Then create your SplashActivity as suggested by Ismail in the link that he has provided

Upvotes: 0

ismail alaoui
ismail alaoui

Reputation: 6073

It is not a good idea to use a splash screen that way . This should be strictly avoided.

With this approach you may also lead the problem of blank white page appears during splash launching and this what exactly happened to you !

i advice you to read this article and try to make your splash screen in the right way to avoid such behavior !

Upvotes: 0

Related Questions