简体   繁体   中英

Switch or Remove Splash Screen from MainActivity

I have a MainActivity that has a long cold start on some devices so I would like to use splash screen and trigger it from a function on my MainActivity to finish.

I use my SplashActivity as the launcher and then load my MainActivity. This works when I set it in SplashActivity, but my SplashActivity ends abruptly and still getting the blank screen on cold start then starting the app main loop.

Below code ends the splash screen soon and runs the MainActivity still with a long cold start blank screen.

I know this will also work with a timeout/timer as I have seen on most answers, but I would like to trigger it inside my MainActivity by using function or once my NativeActivity main loop starts. I am using JNI to call java functions from C++.

Edit: I have also found an alternative solution on using fragments inside MainActivity, but have no idea where to start as the author did not share the solution in detail here:

https://stackoverflow.com/a/44444946/11736918

public class SplashActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

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


My MainActivity just loads another Native.so library.

public class MainActivity extends NativeActivity {

    static {
        System.loadLibrary("MyLib");    
    }

    public void RemoveSplash() {
      // Ideally I will use this to trigger it from my C++ code using JNI.
    }


}

Here is my AndroidManifest.xml

        <activity android:name=".MainActivity"
                android:label="@string/app_name"
                android:configChanges="orientation|keyboardHidden"
                android:screenOrientation="landscape">
            <!-- Tell NativeActivity the name of or .so -->
            <meta-data android:name="android.app.lib_name"
                    android:value="native-activity" />
        </activity>

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

change your code on splash like this:

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

or, you can set the theme of MainActivity from manifest

<activity
        android:name=".MainActivity"
        android:theme="@style/SplashTheme">

with SplashTheme is

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorAccent</item>
    <item name="colorPrimaryDark">@color/colorAccent</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item 
       name="android:windowBackground">@drawable/img_app_splash_screen</item>
</style>

then, call your function before super.onCreate() when your function is finished, call setTheme(R.style.AppTheme) , then call super.onCreate() and setContentView(R.layout.activity_main)

no needs SplashActivity

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