4

I currently have a splashScreenActivity that requires the User the press on a button to go to the MainActivity.

Would it be possible to load all the contents of MainActivity WITHOUT MainActivity's UI APPEARING ON TOP OF splashScreenActivity's UI so that when he does presses the button, he is redirected to the MainActivity and all the data is 100% loaded?

Thanks in advance

DanielRM
  • 636
  • 7
  • 24
  • use splash screen as Layout over other elements. and on click invisible it. – Elias Fazel Jun 07 '17 at 21:08
  • If you are using service you can start it in the background while you on the splash screen. –  Jun 07 '17 at 21:08
  • @EliasFazel then he will see the splash every time activity created ? –  Jun 07 '17 at 21:09
  • Even if main activity set it's layout to its own xml? I tried asynctask but then the mainActivity's layout file just covers my splashscreen layout, as it loads asynchronously @IbrahimAli – DanielRM Jun 07 '17 at 21:09
  • Please try to have an activity for splash and another one for your mainActivity, so your can load your data in you splash with `IntentService` alternatively. –  Jun 07 '17 at 21:15
  • @IbrahimAli Currently, i have 1 activity for splash and 1 for mainActivity – DanielRM Jun 07 '17 at 21:24
  • then use `IntentService` instead of asyncTask to load your mainactivity data while you in the splash activity. –  Jun 07 '17 at 21:25
  • @IbrahimAli the problem is that mainactivity runs the following in the onCreate `setContentView(R.layout.activity_main);` which then just makes it come to the foreground before the user even pressed the button in the SplashScreenActivity – DanielRM Jun 07 '17 at 21:44
  • what condition you set to show splash screen or not set it to visible or invisible that layout – Elias Fazel Jun 07 '17 at 21:57
  • @EliasFazel currently im not using splash screen as a layout, im using it as an activity because i've attached java functionality with it – DanielRM Jun 07 '17 at 22:00

3 Answers3

8

I found an answer to my problem!

Note that in my case MainActivity can be any activity

Having a Splash Screen as a fragment instead of an activity allows you to overlay the MainActivity with the fragment, while the MainActivity data loads in the background.

At this point, whenever you are ready, simply set the visibility of the fragment to View.GONE or pop it off the fragment stack getFragmentManager().popBackStack();, and you will return (never really left) to your MainActivity with all the data loaded.

DanielRM
  • 636
  • 7
  • 24
7

Use Full Screen Dialog With Runnable on Main Activity

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);
    }
IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Tarun Valecha
  • 71
  • 1
  • 2
0

enter code here1) Use AsyncTask<> to load all your MainActivity contents in the background while the user is in the SplashScreenActivity. This will help you avoid the extra step of clicking on a button to take you from the SplashScreenActivity to the MainActivity, and this will be handled through the use of Intents. (Refer to working example below)


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>
StealthRT
  • 10,108
  • 40
  • 183
  • 342
nocholla
  • 122
  • 5
  • I have tried this. But i can't load MainActivity's contents without the UI appearing on top of my SplashActivity, maybe i am doing something wrong? – DanielRM Jun 08 '17 at 15:08
  • Create 2 separate layout files for each Activity i.e. (1) MainActivity.java is tied to the activity_main.xml file (2) SplashScreenActivity.java is tied to activity_splash.xml. This will ensure the separation of code and stop your MainActivity Contents from being loaded in the SplashScreen UI. – nocholla Jun 08 '17 at 17:03
  • That's how my setup is currently configured, 1 layout for main, 1 layout for splash, but when i start the main activity with the intent, it calls the setContentView(r.id.******) which is on the onCreate of my MainActivity, and this makes the Main activity overlap the UI of my SplashActivity – DanielRM Jun 08 '17 at 17:15
  • Can you check on both onCreate Methods i.e. for MainActivity and SplashScreenActivity to ensure that their setContentView points to their respective XML files e.g. (1) MainActivity.java points to setContentView(R.layout.activity_main); (2) SplashScreenActivity.java points to setContentView(R.layout.activity_splash); ... Seems like your SplashScreenActivity.java is pointing to setContentView(R.layout.activity_main); ... Thats why its rendering the MainActivity Contents. – nocholla Jun 08 '17 at 17:23
  • yup, the problem is that i must wait for the user to press on the button (while in SplashScreenActivity), so if i load MainActivity asynchronously from my splash activity, when it get's to the point (1) MainActivity.java loads setContentView(R.layout.activity_main); , the UI of mainActivity will overlap the UI of SplashScreenActivity – DanielRM Jun 08 '17 at 17:27
  • Why don't you remove the SplashScreenActivity button or is it part of the Specs? Reason being that my code above loads the SplashScreen for 2 seconds and then switches automatically to the MainActivity. – nocholla Jun 08 '17 at 17:31
  • If it were up to me i would... But my client insists that the button be there -_- , thanks again for taking the time to help, really appreciate it – DanielRM Jun 08 '17 at 17:33