切换或移除主活动的启动画面。

huangapple 未分类评论50阅读模式
英文:

Switch or Remove Splash Screen from MainActivity

问题

我有一个MainActivity,在某些设备上启动时间很长,因此我想使用闪屏界面,并从MainActivity上的一个函数触发它以完成操作。

我将SplashActivity用作启动器,然后加载MainActivity。当我在SplashActivity中设置它时,可以正常工作,但是我的SplashActivity会突然结束,并且在冷启动时仍然出现空白屏幕,然后开始应用程序的主循环。

下面的代码很快结束了闪屏界面,并且仍然有很长的冷启动空白屏幕。

我知道使用超时/计时器也可以解决,因为我在大多数答案中看到过,但我想通过在MainActivity内部触发它来实现,可以使用函数或者在我的NativeActivity主循环启动后。我正在使用JNI从C++调用Java函数。

编辑: 我还找到了一个替代方案,使用MainActivity内的片段(Fragments),但是由于作者没有在此处详细说明解决方案,我不知道从何处开始:

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();
    }
}

我的MainActivity只加载另一个Native .so库。

public class MainActivity extends NativeActivity {

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

    public void RemoveSplash() {
        // 理想情况下,我会在C++代码中使用JNI来触发它。
    }
}

这是我的AndroidManifest.xml

<activity android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape">
    <!-- 告诉NativeActivity .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>
英文:

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(&quot;MyLib&quot;);	
	}

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

 
}

Here is my AndroidManifest.xml

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

		&lt;activity android:name=&quot;.SplashActivity&quot;
					android:theme=&quot;@style/SplashTheme&quot;&gt;
			&lt;intent-filter&gt;
				&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
				&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
			&lt;/intent-filter&gt;
		&lt;/activity&gt;    

答案1

得分: 0

将您的代码在启动界面(SplashActivity)上更改如下:

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);

或者,您可以从清单文件(AndroidManifest.xml)中设置 MainActivity 的主题:

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

其中,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>

然后,在 super.onCreate() 之前调用您的函数,在函数完成后,调用 setTheme(R.style.AppTheme),然后调用 super.onCreate()setContentView(R.layout.activity_main),这样就不需要 SplashActivity 了。

英文:

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

&lt;activity
        android:name=&quot;.MainActivity&quot;
        android:theme=&quot;@style/SplashTheme&quot;&gt;

with SplashTheme is

&lt;style name=&quot;SplashTheme&quot; parent=&quot;Theme.AppCompat.NoActionBar&quot;&gt;
    &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorAccent&lt;/item&gt;
    &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorAccent&lt;/item&gt;
    &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;
    &lt;item 
       name=&quot;android:windowBackground&quot;&gt;@drawable/img_app_splash_screen&lt;/item&gt;
&lt;/style&gt;

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

huangapple
  • 本文由 发表于 2020年4月9日 11:48:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61113647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定