How to start an activity in background and show it afterwards?

后端 未结 3 1041
青春惊慌失措
青春惊慌失措 2020-12-18 01:38

I\'ve two activities, one is the SplashActivity and the other is MainActivity, an activity containing a webview.

I need to load the webvi

相关标签:
3条回答
  • 2020-12-18 01:54

    i hope this should work perfectly...

       ProgressDialog pd;   
    
       pd = ProgressDialog.show(YOUR_ACTIVITY.this,"Loading...", true, false);
    
                new Thread(new Runnable() {
    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
    
                        Intent intent = new Intent(YOUR_ACTIVITY.this,
                                NEXT_ACTIVITY.class);
    
                        startActivity(intent);
                        pd.dismiss();
                    }
                }).start();
    
            }
        });
    
    0 讨论(0)
  • 2020-12-18 01:56

    Android Apps can cache web data. you can use this to your advantage. (and it worked for me). What I did:

    • I created a WebView in the splash screen.
    • didn't attach it to the UI,
    • requested the webpage I needed in the Main Activity.
    • once the WebView in your splash screen is loaded start the other Activity.

    the WebView there will use the cache of the WebView you created in the splash screen.

    0 讨论(0)
  • 2020-12-18 02:11

    You could launch the MainActivity first and start the SplashActivity in onCreate() of MainActivity. After the required duration, you could just close the SplashActivity and MainActivity would reappear so that it would appear as if you have started Main from Splash.

    Let me explain it -

    In your MainActiviy use an intent and start SplashActivity by using startActivity and not startActivityForResult as you would not want to pass back the result from SplashActivity to MainActivity.

    Now that you are in SplashActivity, start a thread and wait in the thread for the required duration and then call finish() so that SplashActivity will close and the previously started MainActivity comes to the foreground.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题