Home > front end >  How to implement Twitter WebView?
How to implement Twitter WebView?

Time:12-31

Hi I am working on twitter WebView. But twitter WebView is not working perfectly. I am facing some issues like

  1. when in click on a tweet it opens on new page but on back press it reload the home page instead of loading the previously loaded home page and scrolling position to from where user clicked on the tweet.

  2. On search page I cant able to search any keyword. when I type any character from softkey board the search page reloads.

Code:

public class MainActivity extends AppCompatActivity {
    private WebView webView;

    @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=findViewById(R.id.webview);
        webView.getSettings().setAppCachePath(getCacheDir().getAbsolutePath());
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
        webView.setWebViewClient(new WebViewClient());
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.setCookie("https://twitter.com/","uniqueId=10028; Domain=.codean.app");
        String cookie = cookieManager.getCookie("https://twitter.com/");
        webView.loadUrl("https://twitter.com/");
    }
}

CodePudding user response:

  1. For existing , you need to use flag on your back pressed here is your sample code

     public class MainActivity extends AppCompatActivity {
    
     WebView webView;
     static boolean flag = true;
    
     @SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"})
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         webView=findViewById(R.id.web_view);
         webView.getSettings().setAppCachePath(getCacheDir().getAbsolutePath());
         webView.getSettings().setAllowFileAccess(true);
         webView.getSettings().setAppCacheEnabled(true);
         webView.getSettings().setJavaScriptEnabled(true);
         webView.getSettings().setDomStorageEnabled(true);
         webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
         //webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
         webView.setWebViewClient(new WebViewClient());
         CookieManager cookieManager = CookieManager.getInstance();
         cookieManager.setAcceptCookie(true);
         cookieManager.setCookie("https://twitter.com/","uniqueId=10028; Domain=.codean.app");
         String cookie = cookieManager.getCookie("https://twitter.com/");
         webView.loadUrl("https://twitter.com/");
         if(flag)
             Log.d("ONCREATE", "flag is true");
     }
    
     @Override
     public void onBackPressed() {
         if (webView.canGoBack()) {
             webView.goBack();
             Log.d("ONBACK", "CALLED" );
             flag = false;
         } else {
             super.onBackPressed();
         }
     }}
    
  2. Not Able to search keyword , This line of code creating issue ,You need to remove this line and its working fine.

    webView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");

CodePudding user response:

1- I think the problem you are facing of going back in webview you can use like below.

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}

2- Did you search by using webview clicking or softkeys done/search IME?

CodePudding user response:

When your WebView overrides URL loading, it automatically accumulates a history of visited web pages. You can navigate backward and forward through the history with goBack() and goForward().

For example, the following shows how your Activity can use the device Back button to navigate backward:

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

The canGoBack() method returns true if there is actually web page history for the user to visit. Likewise, you can use canGoForward() to check whether there is a forward history. If you don't perform this check, then once the user reaches the end of the history, goBack() or goForward() does nothing.

  • Related