在闪屏界面期间加载MainActivity

4
我目前有一个名为 splashScreenActivity 的活动,需要用户按下一个按钮才能进入 MainActivity
是否可能加载 MainActivity 的所有内容而不在 splashScreenActivity 的 UI 上覆盖 MainActivity 的 UI,以便当他按下按钮时,重定向到 MainActivity 并且所有数据都已经100%加载?
提前致谢。

请尝试为闪屏界面和主界面分别创建活动,这样您就可以使用IntentService在闪屏界面中加载数据。 - user6490462
@IbrahimAli 目前,我有一个启动画面的活动和一个主活动。 - DanielRM
在闪屏界面中,如果你需要加载主界面的数据,建议使用IntentService而不是AsyncTask - user6490462
你需要设置什么条件来显示启动画面,或者将其设置为可见或不可见的布局。 - Elias Fazel
@EliasFazel 目前我没有将启动屏幕作为布局使用,而是将其作为活动使用,因为我已经将 Java 功能与其连接了起来。 - DanielRM
显示剩余5条评论
3个回答

9
我找到了解决问题的方法!
请注意,在我的情况下,MainActivity 可以是任何活动。
将启动画面作为一个 fragment 而不是一个 activity,可以让你将 MainActivityfragment 叠加在一起,同时后台加载 MainActivity 数据。
此时,当你准备好时,只需将 fragment 的可见性设置为 View.GONE,或从片段堆栈中弹出它 getFragmentManager().popBackStack();,然后你将返回(实际上从未离开)到已加载所有数据的 MainActivity

7
在主活动中使用全屏对话框和可运行对象。
public void showsplash() {

        final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.activity_splash_screen);
        dialog.setCancelable(true);
        dialog.show();

        final Handler handler  = new Handler();
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                {
                    dialog.dismiss();
                }
            }
        };
        handler.postDelayed(runnable, 30000);
    }

0

输入代码1)使用AsyncTask<>在用户处于SplashScreenActivity时在后台加载所有MainActivity内容。这将帮助您避免点击按钮从SplashScreenActivity转到MainActivity的额外步骤,并且这将通过使用Intents来处理。(请参考下面的工作示例)


SplashScreenActivity.java

        package foo.foo.load.mainactivity
        
        import android.app.Activity;
        import android.content.Context;
        import android.content.Intent;
        import android.content.pm.ActivityInfo;
        import android.content.res.AssetManager;
        import android.content.res.Configuration;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.util.Log;
        import java.util.ArrayList;
        import java.util.List;
        
        public class SplashScreenActivity extends Activity {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_splash);      
        
                // Make call to execute AsycTasks<> here
                // This helps avoid the extra step of clicking on a button
                // to take you to the MainActivity
                new StartMainActivity().execute(this);
        
                Thread timerThread = new Thread() {
                    public void run() {
                        try {
                            sleep(2000);
                        } catch(InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            // After 2 seconds the Splashscreen will disappear and user is taken to MainActivity
                            Intent splashScreenIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
                            startActivity(splashScreenIntent);
                        }
                    }
                };
        
                timerThread.start();
            }
        
            @Override
            protected void onPause() {
                super.onPause();
                finish();
            }
        
            private class StartMainActivity extends AsyncTask<Context, Void, Intent> {
        
                Context ctx;
        
                @Override
                protected Intent doInBackground(Context... params) {
                    ctx = params[0];
                    AssetManager assetManager = ctx.getAssets();
                    final CBLite cblite = new CBLite(new AndroidContext(ctx), assetManager);
                    
                    // Handle all your MainActivity Contents call here 
                    // Begin MainActivity Content Calls            
                    Image.getImages();
                    Navigation.getMainNavigation();
                    // End MainActivity Content Calls
            
                    Intent in = new Intent();
                    in.setClass(ctx, MainActivity.class);
        
                    return in;
                }
        
                @Override
                protected void onPostExecute(Intent intent) {
                    ctx.startActivity(intent);
                    super.onPostExecute(intent);
                }
        
            }
        }
    

activity_splash.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:background="@drawable/splashscreen_background_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/splashScreenMainTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="150dp"
            android:gravity="center"
            android:text="Header Title"
            android:visibility="visible"/>
    
        <TextView
            android:id="@+id/splashScreenSubTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splashScreenMainTitle"
            android:layout_marginBottom="40dp"
            android:gravity="center"
            android:text="Sub Header Title"
            android:visibility="visible"/>
    
        <ImageView
            android:id="@+id/splashScreenLogo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splashScreenSubTitle"
            android:src="@drawable/logo"
            android:layout_gravity="center_horizontal"
            android:background="@android:color/transparent"/>
    
    </RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_menu_search" />    
   </android.support.design.widget.CoordinatorLayout>

我已经尝试过了。但是我无法在SplashActivity的UI出现在MainActivity内容之上的情况下加载MainActivity的内容,也许我做错了什么? - DanielRM
这是我的设置目前的配置,一个主要布局,一个闪屏布局。但是当我使用意图启动主活动时,它调用setContentView(r.id.******),这在我的MainActivity的onCreate上,使得Main activity重叠了SplashActivity的UI。 - DanielRM
你可以检查一下两个onCreate方法,即MainActivity和SplashScreenActivity,确保它们的setContentView指向各自的XML文件,例如(1) MainActivity.java指向setContentView(R.layout.activity_main); (2) SplashScreenActivity.java指向setContentView(R.layout.activity_splash);...似乎你的SplashScreenActivity.java指向了setContentView(R.layout.activity_main);...这就是为什么它渲染了MainActivity内容的原因。 - nocholla
是的,问题在于我必须等待用户在SplashScreenActivity中按下按钮,因此如果我从我的闪屏活动异步加载MainActivity,当它到达(1)MainActivity.java加载setContentView(R.layout.activity_main);的点时,mainActivity的UI将重叠在SplashScreenActivity的UI上。 - DanielRM
为什么不删除 SplashScreenActivity 按钮,或者它是规格的一部分吗?原因是我的上面的代码会加载 SplashScreen 2 秒钟,然后自动切换到 MainActivity。 - nocholla
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接