Android: how to pass webview in intent.putExtra - android

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.

Related

How can I show two activities on the same screen?

I am trying to find a way to show two activities on the same screen, I have two activities main and content activity, the main activity has a mini-player that's implemented and only works in an activity and the content activity has some implementations that also only work in an activity so the solution of converting them into a fragment does not work for me. I need to find a way to show the mini-player in the main activity inside of the content activity. I have looked online and none of the solutions so far have been working for me!
You have 2 ways to complete task.
Fragments, They can do same things as activity. Its lite weight activity class with more flexible to attach and detach from the screen.
Views, If you think your task can be done by only activity then use views for it. you can apply different animations and show/hide feature to display content on the screen within single activity.
Fragment is a part of an activity, which contributes its own UI to that activity. Fragment can be thought like a sub activity. Where as the complete screen with which user interacts is called as activity. An activity can contain multiple fragments.Fragments are mostly a sub part of an activity.
From document
For example, a news application can use one fragment to show a list of
articles on the left and another fragment to display an article on the
right—both fragments appear in one activity, side by side, and each
fragment has its own set of lifecycle callback methods and handle
their own user input events. Thus, instead of using one activity to
select an article and another activity to read the article, the user
can select an article and read it all within the same activity
Use Fragment.Fragment are Runs inside the Activity.
you can display multiple fragment in same time.
Refer Fragments
Create a fragment
At first, Android system can show only one activity on the screen.
You should not use Activity.
Please use Fragment. You can use multiple fragment on the same screen.

How to use multiple Fragment in single activity in different times

I am making an app in which the header and footer would be same through out the app. i can start another activity with the same pattern of design in header and footer but as the other activity starts there comes a delay and we can easily notice that a new activity has pop up.
So what I want :
I want that if user click on any button from the footer the content in the middle between header and footer should be change and user should not see any jump which is made when I move to other activity.
So for this I know that I can use fragments. But for some reasons I can not use fragment activity. All I want to move fragment in the center only. So How can i do that . and How can I initialize other things which are in new fragment let say the upcoming fragment could have button so in this way , I wanted to know would it make my main activity heavy which is calling all these fragment
So please guide me through the demo code .
Note: please do not refer me to a fragment link just past some code to give me some idea.
Will be easiest to have the activity implement the header and footer and the variable content as fragments.
Please refer to this post. It has everything you need
Fixed header and footer in android app

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.

display large string into multiple pages in single activity

I am very new to android development. I am trying to create a simple two activities based ebbok type application where activity1 is table of content and activity2 displays the details. I have a listview in Activity1 where I list the table of content. On clicking any specific content, I load activity2. In onCreate of activity2, I load the content into a string(from a txt file in assets). and display that in textView (as defined in activity2.xml). So far so good. The challenge now is to display the content page-wise because, as of now all of my string gets loaded into a single page in activity2. So I added two buttons at the bottom of activity2 (defined that in activity2.xml). My planned logic now is
get the screen height (I could get that)
get the botton height and substract 2) from 1). Now I cannot do this becuase I cannot get button height in onCreate of Activity2
and then find how much text I can fit in one page, find number of pages accordingly, use a page index and using a page delimiter, keep loading remaining text into next pages when user clicks "next" button.
So i need help with
-how to get button width as I have mentioned in 2)
-and any sample code you can share to perform 3) above will also help me.
any help here from any one of you experts out there will be much appreciated.
Using Fragements will Solve your problem.
Fragements :
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity
For More About fragements just go through this link.
Hope it will help you.
I figured out a much efficient way to display a long string across number of pages. -I display the long text into the text view and disable scrolling -I then find out the lines in the layout and lines on single screen, hence I know how many screens(pages) will the long string span across -Then whenever user clicks a button to go to next page or swipes screen to go to the next page then I use TexView.scrollTo() function to move to number of lines(as many on a screen) forward. So it becomes as good as moving to next page

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