SpecialEd
SpecialEd

Reputation: 495

Android: Create an Activity initially hidden, process data while its hidden, then show it only after its ready

I have a two activity application. One is a "HomePage". Another is a "DataPage" activity that just contains a webview. When the user clicks on a button on the HomePage, I show the DataPage and load the URL. The problem is the URL is a very slow loading page. I want to solve that problem by creating both activities when my app is open. The home page will be visible, the DataPage will be hidden. I will immediately start the WebView of the DataPage so that it loads in the background. Then when the user clicks on the button, I will reveal the DataPage, with most of its data already loaded.

I dont know how to create an activity in an initially hidden state. And then bring that activity into the front? Is there a way of doing that?

Upvotes: 2

Views: 6885

Answers (2)

Eric Levine
Eric Levine

Reputation: 13564

If your Activity is not in the foreground, then it either has never been created, its in the Paused state, or in the Stopped state. You won't be able to have it render itself in the background during any of those times. You can read more about that here which contains the following flow chart of an Activity's lifecycle:

Activity Lifecycle Flowchart

There are a variety of ways to retrieve data through a background thread. Would it be possible to download the page as a locally cached file, so the DataPage only needs to render the HTML/CSS/JavaScript when it opens? If the rendering page is the bottleneck, this won't help you much though.

Upvotes: 3

twaddington
twaddington

Reputation: 11645

You can't have an Activity that runs in the background, that's what a Service is for. I'd recommend that when your "DataPage" Activity is started, you default the visibility of the WebView to gone and display a ProgressBar (loading spinner) to indicate that something is happening.

You can use the setWebViewClient method on your WebView to provide a custom client that overrides the onPageFinished method. When that callback method is triggered you can hide the progress bar (spinner) and show your WebView.

Something like this:

WebView view = findViewById(R.id.my_webview);
view.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        view.setVisibility(View.VISIBLE);
    }
});
view.loadUrl("http://mydomain.com/");

Upvotes: 1

Related Questions