Preload URL in webview in the next activity - android

I have 2 Activities. In the second Activity I have a WebView in which I load a local html page(from the assets folder). In the onCreate method I call webView.loadUrl(url).
I need a slide in transition from the first Activity to the second. And this is where my problem comes in: The second activity slides in as it should, but it takes a moment to show the page in the WebView. So, basically, there's just a white empty screen that slides in.
I need the second Activity to load the html page before it slides in.
How do I do that?

An alternative would be to move the contents of your second activity (WebView etc) into a Fragment and push it into the existing Activity. That way you could preload the WebView into the fragment before showing it.

The short answer is: you can't really.
only one Activity is "active"(on the screen) at a time, because of this your SecondActivity cannot be doing anything while your FirstActivity is still visible.
You can achieve a similar effect as what you are after if you use only 1 Activity with 2 WebViews, one visible, and one hidden. You should be able to load a url in the second (hidden) webview while its hidden, and then make it visible and slide it in whenever you are ready.

Related

setOffscreenPageLimit loads all fragments together?

I have an android app with a MainActivity which implements a menu with 5 buttons. Each button activates a different fragment.
Without using the "setOffscreenPageLimit(int limit)", every time I go from fragment to fragment, each fragment loads every single time.
Using:
setOffscreenPageLimit(5)
I understand that loads all of my 5 main fragments when starting my application. But I don't want this, because it is too heavy.
I want to implement this:
"Load every fragment only when the user activates it, through choosing the menu button.Then keep it on memory , so when user goes back to this fragment, you don't have to load it again."
From the scratchy details you have provided, I understand that, you want to show fragment when it's displayed on the screen, you don't want it to load when fragment is in background. For this you can try this:
https://stackoverflow.com/a/25001920/6383029

How to move back and forth among many images on a click of back and forward button in android?

I am creating a book app in which i have many text images around 60. I want to move back and forth on click of forward and backward button, just to give changing pages feel where user can read the pages.
Also i am not sure whether to use fragements or a whole activity to show the images.
If the content is static, I do believe the best approach would be to make an array with pointers to each image, then, have an activity that loads the image and make an animation to "swap" pages.
Also, how is the hierarchy you are using? Does the user use the hardware back button to go back pages, or are you using left/right buttons to swap pages.
In the first case, do note that you could create a lot of activities that serve no purpose.
So:
onCreate() Start the program, do your styles and etc. then load the image[0]
onResume() Update the image, do the transition effect
onPause() Save the index of the image array, and the direction the user is going (left/right), then decrement/increment the value, and wait for the onResume, or start this activity again with the android:launchMode="singleTask" modifier in that "view book page" Activity
onDestroy() Destroy everything you created to free the memory.

Prerender and prescroll of webview content

I have an application using fragments. One of these fragments a WebView which show some locally generated html content. It could have different size but not too large (1-30kb). When user clicks a button on one fragment (let it be FragmentA) i need to change it with that WebView fragment (FragmentB).
How is it going now:
1. Instaniate FragmentB
2. replace FragmentA with FragmentB (user sees blank white screen)
3. FragmentB loads generated html, render it (user sees top of html)
4. Page scrolls down with webView.pageDown(true) (calling it from opPageFinished()) (user sees scrolling animation to the bottom of the page)
How i want it to be:
1. Instaniate FragmentB
2. set it to some invisible container (user still have FragmentA on the screen)
3. it renders html, scrolls it down (same)
4. when page is ready, rendered and scrolled down I hide FragmentA and show FragmentB
Any suggestions how I can achieve that?
I tried the second algorithm I described but the problem that it looks like WebView dont want to render and\or scroll content untill the fragment is visible to user.
After the user pressed the button in Fragment A, then I would switch to Fragment B immediately.
Fragment B would contain your webview, and something that overlaps the webview to hide it. It could be anything, even a simple image, or a loading animation. In onPageFinished(), you should call to WebView.pageDown(true), and hide the overlapping layer to show the webview.
Okay, I figured it out myself. The answer is simple.
I'm placing FragmentA and FragmentB inside FrameLayout so that FragmentA is "on top" of FragmentB. So when I need to show B i'm just hide A. That's all. B is always "visible" and can render its content in background behind A.

Android: how to pass webview in intent.putExtra

I'm new to Android and am trying to make a simple app.
I want to create an activity which displays a webview loaded with an html. For the user experience to be good, I want to preload the html into the webview and pass that to the activity that displays it. I'm not able to find any examples online which show how to pass webviews into the putExtra method. Any ideas?
#CommonsWare is right; you'll want to manage a hidden WebView on your page, load it, and then show it when the user wants to see it. This thread has some code that seems pretty close to what you are looking for.
I have 2 activites: the first one preloads a url into a webview; the second one adds the webview to a layout and sets the content view with the layout.
That is not going to work. Just have one activity.
I want to pass the empty webview through intent.putExtra into the first activity.
That is not possible. Just have one activity.
I want to pass it into the second activity for adding it to the layout and setting the content view
One activity cannot show another activity's views. Just have one activity.

switching between webview activity without reloading the page

Here is i wish to do.
I want to have multiple webview loading their own page. Those webviews are created in different activies that are resulted from item click on a listview.
Sometimes i want to switch from one webview activity to another.
What i have attempted shows that switching between those activities always called onCreate() method that calls webView.loadUrl(). Therefore, the web page get reloaded everytime i switch.
Anyone have idea to design such activities navigation? The key is how to switch to an existing activity without reloading the webView inside. Thank you for advances
If you create an activity for each webView there is no possibility to do what you describe. this happens since you close each activity and give an "OK" to the GC to collect it and throw all WebView data.
What you need to do is to create some kind of a web container that will hold a predefined amount of WebViews (don't keep more than ~6 alive, it will eat up you device's memory).
Whenever you press your list item, start loading a WebView in your container, and make it visible on top of your current Layout.
When you press back, just set visibility of the web container to GONE.
If you try to open a 7th WebView, just kill the first one, and continue.
This method will assure you that you have live WebViews that do not need to be loaded again...
Hope this helps.
You can do this with a ViewAnimator. Create your WebViews and add them to the ViewAnimator with (something like)
mViewAnimator.addView(mWebView0, index_0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
then switch to a particular WebView by index (something like)
mViewAnimator.setDisplayedChild(index_0);

Categories

Resources