I have a simple android app with WebView which opens the site main page. I need to save WebView state when the user presses "home" or "main menu" buttons. And then, when user opens app from the homescreen or main menu I need to load this saved state.
Everything works fine except the first launch (after installation of built apk file): if I open app from main menu the WebView restores just fine, but if I try to open it from homescreen it starts to reload. Is there any ways to fix that? Thanks in advance.
P.S. if I install app from Android Studio (not from built apk file) everything works as I expect, so I am confused.
These are methods where I save and restore WebView:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
if (savedInstanceState != null)
webView.restoreState(savedInstanceState);
else
webView.loadUrl(getString(R.string.URL));
}
protected void onSaveInstanceState(Bundle outState)
{
webView.saveState(outState);
super.onSaveInstanceState(outState);
}
Related
I have a problem with showing simple google.com in my WebView.
Tried this on few emulators and 2 phones, all the same, something is wrong with my code.
1. Issue
First screen shows normally google's asking user to accept their legal stuff (1st image).
After accepting it moves to google's main page. But only lower part (Country, settings, adds, about etc.) is shown (2nd image). There is no Google logo, no search bar and buttons and no stuff from the upper display (like GMAIL, GRAPHICS, ACCOUNT and so on).
1ST SCREEN OF GOOGLE.COM
INCORRECTLY SHOWN GOOGLE PAGE
2. Code
I added permission in Manifest.
Trying to solve the issue I stackoverflowed below two lines and added to Manifest, but to no avail:
android:hardwareAccelerated="true"
android:usesCleartextTraffic="true"
**
3. My java code is below:**
(each of the settings I tried to comment out and then one by one uncomment, but still no result).
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.webView);
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
webView.loadUrl("http://www.google.com");
}
}
Could someone please check this is tell me what I've done wrong here?
Stupid thing, should not have done this late into the night.
The problem was only with layout, rest of the page was out of screen.
Needed only to match the WebView to constraints.
With Android 5, Google supports multiple activities in the recents screen for a single application.
I managed to implement multiple activities in the recents screen with this official doc.
How can I change the title of the activity? Switching to recents screen always shows my app name as title.
Activity.setTitle("title");
changes the title on the toolbar, but I want it to change in the recents screen, just like Google does in Google Chrome App.
I need to change the title programatically.
In your activity you can use new method:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout);
setTaskDescription(new ActivityManager.TaskDescription("Activity 2"));
}
setTaskDescription sets information about activity in recents task list. In this example only title.
I have a simple android application, an activity with a WebView as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gfx);
WebView browser = (WebView)findViewById(R.id.webView1);
browser.loadUrl("http://www.somepage.com");
}
My internet page is being changed by some ajax actions, first it shows a progress bar then it redirects to another page.
What happens to me that my browser stays in the first page, and doesn't respond to the changes in the page, it doesn't act like a real browser.
If I open the page directly in the browser using:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.somepage.com"));
startActivity(browserIntent);
it works fine.
Yo guys, this is bugging me and I can't find a fix for it,
I have a simple activity to launch a WebView and display a HTML file
public class HelpViewer extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page_layout);
WebView browser = (WebView) findViewById(R.id.help_page_webview);
browser.loadUrl("file:///android_asset/help.html/");
}}
The help_page_layout is your standard WebView resting in a LinearLayout - Not problems there.
The help.html is sitting under the /res/raw/ directory. From what I have see online, there shouldn't be a problem here either.
But when the WebView goes to load, it just tells me that the WebPage was not available because help.html couldn't be found.
What am I doing wrong?
This question suggests the file should be in /assets rather than /res/raw. You also have a trailing slash on the end of your URL.
I've set set files to localhost an set the href links for the epub files ..
more-utopia <br>
and I've try to browse from my custom web view .. but when I click to download the link , it appears no download .. and open the file in the browser with custom web view.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView web = (WebView) findViewById(R.id.webview);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://10.0.2.2/epub");
web.setWebViewClient(new myWebView());
}
class myWebView extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
url = "http://10.0.2.2/epub/";
view.loadUrl(url);
return true;
}
}
What I want to do is to download the files and want to set path the download location to user selected location .
Any helpful tips are humbly welcome.
Your code is set up to prevent people from downloading anything. Every time they click the link, you have them load the original Web page again. Try using setDownloadListener() on your WebView instead. Or, change your shouldOverrideUrlLoading() to do what your question says you want it to do.