简体   繁体   English

从 MainActivity 切换或删除启动画面

[英]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.我有一个 MainActivity 在某些设备上冷启动时间很长,所以我想使用启动屏幕并从我的 MainActivity 上的 function 触发它来完成。

I use my SplashActivity as the launcher and then load my MainActivity.我使用我的 SplashActivity 作为启动器,然后加载我的 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.这在我在 SplashActivity 中设置时有效,但是我的 SplashActivity 突然结束并且在冷启动时仍然出现空白屏幕,然后启动应用程序主循环。

Below code ends the splash screen soon and runs the MainActivity still with a long cold start blank screen.下面的代码很快结束了初始屏幕并运行 MainActivity 仍然带有长时间的冷启动空白屏幕。

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.我知道这也适用于超时/计时器,正如我在大多数答案中看到的那样,但我想通过使用 function 或一旦我的 NativeActivity 主循环开始在我的 MainActivity 中触发它。 I am using JNI to call java functions from C++.我正在使用 JNI 从 C++ 调用 java 函数。

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:编辑:我还找到了在 MainActivity 中使用片段的替代解决方案,但不知道从哪里开始,因为作者没有在这里详细分享解决方案:

https://stackoverflow.com/a/44444946/11736918 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.我的 MainActivity 只是加载了另一个 Native.so 库。

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这是我的 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或者,您可以从清单设置 MainActivity 的主题

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

with SplashTheme is SplashTheme 是

<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)然后,当您的 function 完成时,在super.onCreate()之前调用您的 function ,调用setTheme(R.style.AppTheme) ,然后调用super.onCreate()setContentView(R.layout.activity_main)

no needs SplashActivity不需要 SplashActivity

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

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