switching between webview activity without reloading the page - android

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);

Related

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.

Android ViewPager - Save WebView input

I'm new to the Android ViewPager.
I have a ViewPager with WebViews on each page - basically slides of web pages. Some of the pages have text input. I want to save the user input of the pages into a Java object, so I can send them to the server, as well as restore them later when reopening the ViewPager.
Any idea how to go about doing this? Originally I thought I should save whenver I change pages (OnPageChangeListener), but I can't figure out how to pull up the Fragment of the last page, so I can get hold of the input fields from the WebView to save. I need the save to occur so that the ViewPager always displays the correct data, e.g. when sliding between pages, switching orientations, pause/resume app, etc.
Thanks very much in advance.
well can go like this
Catch the user input using javascript bridge,have a database to store all the user inputs.
Then use the same user inputs while sending to server, or while restoring the webpages.
OK, I think I have figured it out:
Rather than saving when flicking between pages, I'm now saving the data in the onPause() of the Fragment itself, i.e. the Fragment for each page. The Fragment's onPause() is called when:
The Fragment is about to be destroyed (when exiting the ViewPager, or when the Fragment is outside of the ViewPager's cached range)
When the app itself is paused, e.g. switching to another app, or when it's killed
When a child Activity is opened
When the orientation of the device changes, causing the parent Activity to be recreated
This seems to the most reliable way of saving a Fragment's data.
Any comments or suggestions on this?
Thanks again.

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.

Preload URL in webview in the next activity

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.

Prevent WebView reloads in FragmentPagerAdapter?

I have a FragmentPagerAdapter used to show about 6 tabs, all of which load their data from a web server. One of the tabs contains a WebView that loads an image from my server. The server side costs of generating the image are high, and thus I want to reduce the number of calls to reload the WebView. For the non-WebView tabs, I have been able to save my state (for those, just a simple array) and restore them as tabs get swiped through.
Problem:
WebView reloads every time I swipe back to it using FragmentPagerAdapter, leading to high reload times and high load on my web server.
Solutions Considered:
Use ViewPager.setOffscreenPageLimit()
This is problematic because it will force more tabs to be loaded, even if they are never going to be viewed. This is needlessly expensive on my server.
Use WebView.saveState() and WebView.restoreState()
The documentation has been updated to make it clear display state is no longer maintained here, so this is no longer useful for this scenario.
Set my activity to have: android:configChanges="keyboardHidden|orientation|screenSize"
This works for the rotation case, but doesn't affect the ViewPager/FragmentPagerAdapter swiping through tabs case.
It sounds like the old behavior of WebView.saveState() would have been perfect...
The problem appears to be that you are wiping out your own results.
While you need a new WebView on a configuration change, you do not need a new WebView otherwise. And, if you already have the WebView, you do not have to tell it to do anything.
So, I'd try this:
Hold onto the WebView that you create in onCreateView() in a data member of the fragment
Only inflate the layout in onCreateView() if that data member is null
Only call loadUrl() if you inflated the layout
(if you are creating the WebView in Java code, replace "inflate the layout" with "create the WebView via its constructor")
If the contents of the fragment is more than the WebView, you will also need to hold onto the root view that you inflated in a data member, so you can return that from onCreateView().

Categories

Resources