Issue
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.
Solution
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;
}
}
}
Answered By - Eric B.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.