Varshil shah
Varshil shah

Reputation: 268

how to set some work in background when splash screen displayed

I want to do some work like fill hash-map when splash screen was displayed. but i don't know how i do it

I think one way is create new thread?

code of my splash screen

public class SplashActivity extends AppCompatActivity {

    long Delay = 2000;
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashactivity);
        StartAnimations();
        Timer RunSplash = new Timer();

        TimerTask ShowSplash = new TimerTask() {
            @Override
            public void run() {
                finish();
                Intent myIntent = new Intent(SplashActivity.this,LoginEnquiryTab.class);
                overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_left);
                startActivity(myIntent);
            }
        };

        RunSplash.schedule(ShowSplash,Delay);
    }

Upvotes: 0

Views: 416

Answers (1)

architjn
architjn

Reputation: 1482

Don't create a separate activity for splash screen. Your splash screen should be on starting activity only.

styles.xml

<style name="AppTheme.Splash" parent="AppTheme">
    <item name="android:windowBackground">@mipmap/ic_launcher</item>
</style>

AndroidManifest.xml

<activity android:name="com.architjn.example.ui.activity.MainActivity"
    android:theme="@style/AppTheme.Splash">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    //do your work here while splash shows..
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

Google uses the above way as well.

Upvotes: 1

Related Questions