3

In the login screen (LoginActivity) of my Android app, there is a 'Sign up' button that takes the user to a registration webpage that is displayed in a WebView (in SignupActivity). After the user has registered (i.e. clicked the 'CreateAccount' button), I would like to take them back to LoginActivity.

My current code is below. It works the first time the app is installed, but after repeating the process the 2nd time, the if/else clause doesn't trigger at all (neither of the Logs shows up and the redirection doesn't happen). Why?

public class SignupActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("SignupActivity", "Signup Activity started");

    WebView webView = new WebView(this);
    setContentView(webView);

    webView.setWebViewClient(new MyWebViewClient());
    webView.loadUrl("https://commons.m.wikimedia.org/w/index.php?title=Special:CreateAccount&returnto=Main+Page&returntoquery=welcome%3Dyes");

}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.equals("https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes")) {
            // Signup success, so load LoginActivity again
            Log.d("SignupActivity", "Overriding URL");

            CookieSyncManager.createInstance(getApplicationContext());
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.setAcceptCookie(false);
            cookieManager.removeSessionCookie();

            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            return true;
        } else {
            Log.d("SignupActivity", "Not overriding URL, URL is: " + Uri.parse(url).getHost());
            return false;
        }

    }

}

And in LoginActivity I have attached this method to a button click:

//Called when Sign Up button is clicked
    public void signUp(View view) {
        Intent intent = new Intent(this, SignupActivity.class);
        startActivity(intent);
    }

However I am new to WebViews so I am happy to change this setup if necessary.

misaochan
  • 890
  • 2
  • 8
  • 25

3 Answers3

1

You should simplt override the shouldOverrideUrlLoading method in your WebViewClient. Then when the user signups, it should be redirected to let's say a success page. So you know the url of success is https://www.example.com/success. In your shouldOverrideUrlLoading check that if the url about to be loaded is the success URL. If it is, then just finish this activity and do whatever you want.

EDIT:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Log.d("SignupActivity", "Loading URL: " + url);
        if (url.equals("https://commons.m.wikimedia.org/w/index.php?title=Main_Page&welcome=yes")) {
            // Signup success, so load LoginActivity again
            Log.d("SignupActivity", "Overriding URL");
            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(intent);
            return true;
        } else {
            Log.d("SignupActivity", "Not overriding URL, URL is: " + Uri.parse(url).getHost());
            return false;
        }

    }
}
Eric B.
  • 4,622
  • 2
  • 18
  • 33
  • Ah, I've tried this and it doesn't seem to work well. I tried signing up from a mobile browser, noting the URL that the user gets redirected to, and then using that with shouldOverrideUrlLoading. Unfortunately it doesn't even seem to detect any URL. I'll paste my code in the main post. – misaochan Aug 09 '16 at 04:10
  • Do 2 things. 1) For debugging log the url in your method. 2) The matcher should match the whole url and not just the host. See my edit. – Eric B. Aug 09 '16 at 04:59
  • Getting much closer now, thank you! This works, but only with a fresh install of the app, oddly?? If I repeat the sign up process a 2nd time, the WebView doesn't detect any URL again. Not sure why? – misaochan Aug 10 '16 at 06:34
  • It may be highly possible that cookies are created for your WebView. Try clearing them before redirecting the user to the LoginActivity. – Eric B. Aug 10 '16 at 07:10
  • Do you mean like this (modified my opening post with the cookie removal code)? The issue persists. :( – misaochan Aug 11 '16 at 05:16
  • See [this](http://stackoverflow.com/questions/28998241/how-to-clear-cookies-and-cache-of-webview-on-android-when-not-in-webview). I think you are removing cookies on devices after Android L and they are not being cleared. – Eric B. Aug 11 '16 at 05:23
0

There is simple approach you can follow for this situation.And that is when you click on "Signup" Button open new activity(WebActivity) from your LoginActivity.

You can add LoginActivity as ParentActivity for WebActivity in your manifest.

So, when you click on signup button and go to WebActivity, do not call finish() method because it will destroy your LoginActivity.

Now, When you press Signup button, WebActivity will open and LoginActivity on backstack And whenever you press back button in WebActivity that time super.backpressed() method redirect you to LoginActivity.

Update(After Read comment) :

If you want to redirect user to LoginActivity then you can call Backpressed() method when user click on "Create account button".Which should be in your Activity.

Pranav P
  • 1,755
  • 19
  • 39
  • Thanks, but I want them to be sent to LoginActivity after they click the "Create Your Account" button within the WebView, not just if they press the back button (though I do want that as well). – misaochan Aug 05 '16 at 06:17
  • Then you can call backpressed() method in the On click of "Create Your Account". It will call if method which called when someone press back button. – Pranav P Aug 05 '16 at 06:19
  • "when user click on "Create account button"" -> What method will I use to check for this? – misaochan Aug 09 '16 at 04:15
0

I think you need JavascriptInterface to do this.

After reading your question, I think the main point of your question is that "How to close the WebView and bring the user data to native".

Take the user to a web page is all depends on your logic, so you need to consider how to deal with it.

But if you want to take the user back, you need to interact with the web page. After user's registration, the web page tells you whether the registration is successful or not, and the data about the user.

Call this function to add a JavascriptInterface to the WebView:

WebView.addJavascriptInterface(Object object, String name)

After registration, web page use JS to call your native method, and in the method, you can get the result of the registration and do what you want.

You may need to read this for more information about JavascriptInterface.

L. Swifter
  • 3,179
  • 28
  • 52
  • I've read up on this method and it appears I need to be modify the webpage code to use this? I don't have control of the webpage source code, I can only modify the app's code. – misaochan Aug 09 '16 at 04:16
  • You need to modify the web code for this method. I'm sorry that my answer doesn't help you. – L. Swifter Aug 10 '16 at 01:49