首页 > 解决方案 > 从“启动屏幕”活动转换在回调中不起作用

问题描述

我目前正在开发一个带有“启动屏幕活动”的 Android 应用程序。在启动屏幕活动onCreate方法中,我使用此方法从缓存中异步加载数据:

void NewslistViewModel.fetchFromCache(Runnable onFinished)

现在我想在缓存加载过程完成后使用onFinished回调导航到下一个:Activity

public class SplashScreenActivity extends DaggerAppCompatActivity {

    @Inject
    ViewModelProviderFactory _providerFactory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
   
        // the real code decides to which Activity to go next here
        // ...
        Intent intent = new Intent(this, MainActivity.class);

        NewsListViewModel newsListViewModel =
            new ViewModelProvider(this, _providerFactory).get(NewsListViewModel.class);

        newsListViewModel.fetchFromCache(
                () -> {
                    Intent[] intents = TaskStackBuilder.create(this)
                            .addNextIntentWithParentStack(intent)
                            .getIntents();
                    if (intents.length > 0) {
                        intents[0].setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    }
                    runOnUiThread(() -> {
                        SplashScreenActivity.this.startActivities(intents, ActivityOptions.makeSceneTransitionAnimation(SplashScreenActivity.this).toBundle());
                    });
                });        
    }
}

我在这里遇到的问题是,现在启动画面并没有自行完成。在我物理触摸设备上的屏幕后,设备的反应就好像它已经在里面一样MainActivity,所以这似乎只是一个显示问题(?)。

我打电话的原因ActivityOptions.makeSceneTransitionAnimation是因为没有它我有丑陋的灰色过渡(如问题所述)。另外,我需要打电话addNextIntentWithParentStack,因为我的应用程序可以通过单击通知来打开,这会引导启动屏幕活动导航到主要活动的子活动。

我的初始屏幕活动定义如下:

AndroidManifest.xml:

<activity android:name=".SplashScreenActivity"
    android:theme="@style/Theme.AppCompat.DayNight.NoActionBar.Launcher">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

样式.xml:

<style name="Theme.AppCompat.DayNight.NoActionBar.Launcher">
    <item name="android:windowBackground">@drawable/splash_screen_background</item>
</style>

splash_screen_background.xml:

<?xml version="1.0" encoding="utf-8"?>

<!-- The android:opacity=”opaque” line — this is critical in preventing a flash of black as your theme transitions. -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">

    <!-- The background color, preferably the same as your normal theme -->
    <item android:drawable="@android:color/white"/>

    <item
        android:width="146dp"
        android:height="201dp"
        android:drawable="@drawable/splash_full_512"
        android:gravity="center" />

</layer-list>

我很确定我在这里做了一些根本错误的事情,所以非常感谢任何帮助。

标签: javaandroidandroid-layoutandroid-activity

解决方案


推荐阅读