1

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>    
ruzip
  • 45
  • 5
  • The whole idea of `SplashActivity` is to show it for a for a while 2 sec lets say .. But you are just launching the `MainActivity` without any delay .. Follow [How do I make a splash screen?](https://stackoverflow.com/questions/5486789/how-do-i-make-a-splash-screen) – ADM Apr 09 '20 at 03:58
  • Because you finish your splash activity when it started. So the splash activity will end abruptly and the MainActivity take a long time to load. Use Thread to load long actions. And runOnUiThread to load MainActivity screen. So user won't see dark screen. – Hacettepe Hesabı Apr 09 '20 at 04:02
  • Use Handler for Activity change delay like this final `new Handler.postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(SplashActivity.this, MainActivity.class); startActivity(intent); finish(); } }, 400);` – Android Geek Apr 09 '20 at 04:10
  • @ADM Thanks I know this, but just to show what I have so far. I already read that as well and I am not going to use timers since the cold start depends on different devices. – ruzip Apr 09 '20 at 04:15
  • @HacettepeHesabı I know the outcome already. How to use runOnUiThread with this? – ruzip Apr 09 '20 at 04:16
  • In my answer, I didn't tell how to do splash activity. I mean if you don't want to wait your user too much, use Thread in your MainActivity. I don't know your MainActivity. So I cannot tell how to use Thread. – Hacettepe Hesabı Apr 09 '20 at 04:21
  • @HacettepeHesabı I see, my MainActivity just loads another .so library, see my updated post. – ruzip Apr 09 '20 at 04:27
  • Then Thread cannot help you. You are right. Use splash screen as answered – Hacettepe Hesabı Apr 09 '20 at 04:38
  • @Android Geek thanks, but I am moving away from timers and would like to trigger it somewhere in my code. – ruzip Apr 09 '20 at 04:51

1 Answers1

0

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

Ade Dyas
  • 53
  • 8
  • Thanks, but I don't want to use delays or timers as it defeats the purpose on what I am trying to accomplish which I would like to trigger it manually on a certain condition by a function. – ruzip Apr 09 '20 at 04:17
  • and, where the function? – Ade Dyas Apr 09 '20 at 04:30
  • See my updated post adding MainActivity. Apparently, that's where I am stuck on what to use inside. Ideally that should end the splash and show the MainActivity already loaded and running. – ruzip Apr 09 '20 at 04:33
  • Thanks, that could work and will try the second option. – ruzip Apr 09 '20 at 04:38
  • Can you write the java part in example or snippet code? It's a bit confusing and not sure where to place them. – ruzip Apr 09 '20 at 04:43
  • seen it, actually I based my current solution on that example. perhaps, I need to load my splash activity on another thread while my main activity loads then end that thread by function? – ruzip Apr 09 '20 at 06:55
  • I still cannot follow your solution without the SplashActivity. Perhaps, there is an existing example out there that I can try. – ruzip Apr 09 '20 at 07:18
  • I will recommend to show a fullscreen dialog instead of fragment. When your lib have ben loaded, you can dismiss the dialog @ruzib – Ade Dyas Jul 07 '21 at 17:57