Current webview with CanGoBack feature inside Tab - android

I am creating application with dynamic tabhost, each tab has dynamic webview using the following tutorial,
http://www.pocketmagic.net/?p=1132
The above is created as Custom control(unable post the source code). I have extended this control with main activity. In keydown event i have checked the webview able to go back or not. If can, then it should load previous url in the current webview. But this is working if i have worked in the same tab. If i have moved two pages and shifted to second tab and then first tab, now pressing back button Application exists. Could you please tell me how to get the current webview to go back.

I have solved this Issue :-)
Solution:
1. Created One class named MyWebView that should extends WebView.
2. Created 4 MyWebView instances from User Side.
3. That MyWebView should have one WebView locally and it should not be static( here i did a mistake).
4. Override the Layout of WebView inside the MyWebView class with Progressbar and WebView inside the FrameLayout.
5. For every tab creation we have to set the Content as instance of MyWebView.
6. Now handle as follows,
UserClass.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webView1!=null)
{
currentWebView= webView1.getWebView();
if(currentWebView!=null && currentWebView.canGoBack() == true)
{
currentWebView.goBack();
}
else
//Show Alert to Quit the Application
}
else
//Show Alert to Quit the Application
return true;
}
}
}
MyWebView.java:
public WebView getWebView()
{
return webview;
}
That is worked for me...

Related

Unable to program detection behavior on Webview and xml Textview

I am trying to change the visibility of my textview inside onCreate method, but I cant seem to get a handle for it. Or, perhaps something else is wrong. Also, the getUrl() from the WebView class is not behaving as it should. It seems that it detects the url i am looking for after i leave the page with the url i am trying to detect. After reading the documentation, it made it somewhat clear based on the definition of the method getUrl(). Here is what the documentation says: "Gets the URL for the current page. This is not always the same as the URL passed to WebViewClient.onPageStarted because although the load for that URL has begun, the current page may not have changed."
Here's what I mean:
I click on "link within the webpage that has url am trying to detect call it url5", then pressed back button, then click on "some other link within webpage call it url6" . Now, when I go back out of link with url6, the link for url5 is detected and I should be able to setVisibility of my textview as VISIBLE, which i tried but nothing happens. Well, here's my code. Please advise of anything that I might be doing wrong.
"xml format"
<RelativeLayout>
specifications for the relative layout...
<TextView>
android:visibility="invisible"
other specifications such as match_parent for both height and width
/>
<LinearLayout>
specifications of linear layout...
<LinearLayout/>
</RelativeLayout>
"inside onCreate"
relative_layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.content_main, null);
text_view = (TextView)relative_layout.findViewById(R.id.text);
WebView w = (WebView) findViewById(R.id.webview);
w.setWebViewClient(new MyWebViewClient());
//outside of onCreate inside private class MyWebViewClient
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
if(W.getUrl().equals("url I want to detect") ||
W.getUrl().equals("another url I want to detect")){
text_view.setVisibility(View.VISIBLE); //HOWEVER NOTHING SHOWS AT ALL
relative_layout.setVisibility(View.INVISIBLE);
}
return false;
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && w.canGoBack()) {
if(text_view.isShown()){
w.goBack();
text_view.setVisibility(View.INVISIBLE);
relative_layout.setVisibility(View.VISIBLE);
}else w.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

Using the Back button in a webview in Android

When using the normal mobile browser, when you press the Back button, it takes you to the previous page. When a WebView is used in my app to display a web page and the user wants to return to the previous page, how is typically done? Does pressing the Back button automatically take the user back to the previous page or does it take them back to the previous Activity? I would assume that since the Back button is normally meant to take the user to the previous activity, it shouldn't be used to return to a previous web page. If this is the case, should my mobile web page include its own Back link that a user clicks on?
I guess what I am trying to do is to understand the correct behavior I should be employing.
Let's look at some popular browser. currently, in my device, there are two browsers
chrome
default internet browser
In both browsers, it goes to the previous page. So it is a common behavior. to do so
you can overwrite the onBackPressed to go to your previous page.
#Override
public void onBackPressed(){
if(mWebView.canGoBack() == true){
mWebView.goBack();
}
else{
finish();
}
}
Please keep in mind that back buttons is coming with majority of the android devices. So you do not need a back button in your activity to go back to previous activity.
And about the webview if you want navigation in the webview to travel all the previous pages then you can put a back button and implement the navigation of the webview using the method webview.goBack().
In short, If you want your user to navigate to previous page then you should give functionality of the webview to go back to previous page and from the first page user click on back button again finish the activity.
You can use this override the onkeydown event and check the key pressed (in this case is back key) and check if is there any previous page
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When you are clicking back button, you are clicking it for activity that is opening the webview better thing would be to give a back button in you layout..and hence saving the last url opened by user
Something like this in activity with WebView:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
or override onBackPressed method:
#Override
public void onBackPressed() {
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
} else {
finish();
}
}

Using one instance webview for multi page

I want to paging a web's content in an android application. For example, i use Flipview to display each page of web content, and i don't know how to using the same webview which i created, loaded url to display on each flipview page.
Anyone as any suggest for me !
Thanks for all suggestions !
use webview.cangoback() property.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
for more details please visit. http://developer.android.com/guide/webapps/webview.html
hope this will help you

Android webview exiting app on backbutton after loadData

I have an application which loads data via Activity --> onStart() --> new LoadTask through AsyncTask --> onPosExecute() --> ListView --> listView.setOnItemClickListener() --> onItemClick() -->Creates Webview
After clicking on the listView, the WebView is invoked to show further details but when you click to go back from the WebView to the listView instead the application exits. Any help would be greatly appreciated on this.
I think you use this code in onItemClick() finish() to finish your activity Remove this(finish();) code when you call the Intent to open the webView
A hacky way around this admittedly odd behaviour would be overwriting the back button. It's generally not accepted when making the app perform not-as-the-user-expects-it-to, but in your case it might be of some help.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
startActivity(new Intent(myClass.this, ListViewClass.class));
return true;
}
return super.onKeyDown(keyCode, event);
}

While using ViewFlipper is it possible to map the back button to go to the previous View rather than back out of the entire application?

My application is set up using ViewFlipper so that I have one main View which links to three other Views, each through their own button. As I've set it up this way, when I'm on one of the three other Views if I press the back button on my android device it backs all the way out of the app. I Just wanted to know if it was possible to map it to just go back to the main View?
Override onKeyDown() :
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//Place your code here!
}
return true;
}

Categories

Resources