Issue
I have created an Android app. Whenever someone opens the website through the app, it goes to the exact link. But if the user opens another link, the app is still on the previous opened link. You can check the app here: https://play.google.com/store/apps/details?id=app.freeairdrop.io
Send these 2 link to yourself on Telegram, or Whatsapp:
Now open the first link in my app, when prompted to choose application. Switch back to Telegram/whatsapp, and click on second link. My app will open, but it's still on that page(first link).Nothing happens, the app is not able to load second link, unless app is closed.
MainActivity.java code:
package app.freeairdrop.io;
import...
public class MainActivity extends Activity{
private ProgressBar progressBar;
private WebView webView;
private SwipeRefreshLayout mySwipeRefreshLayout;
private boolean mShouldPause;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClientDemo());
webView.setWebChromeClient(new WebChromeClientDemo());
mySwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
mySwipeRefreshLayout.setRefreshing(false);
}
}
);
// ATTENTION: This was auto-generated to handle app links.
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();
if (getIntent().getExtras() != null) {
if (appLinkData == null){
webView.loadUrl("https://freeairdrop.io/");
}else
webView.loadUrl(String.valueOf(appLinkData));
} else if (getIntent().getExtras() == null){
webView.loadUrl("https://freeairdrop.io/");
}webView.reload();
}
private class WebViewClientDemo extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toasty.error(getApplicationContext(), "No Internet, pull down to refresh when you're connected to internet", Toast.LENGTH_LONG, true).show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
if (uri.getHost() != null && (url.startsWith("https://freeairdrop.io/") || url.startsWith("https://www.freeairdrop.io/"))) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else {
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
// This method is used to detect back button
public void onBackPressed() {
if (this.webView.canGoBack()) {
this.webView.goBack();
return;
}
else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
AndroidManifest Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="app.freeairdrop.io">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:appCategory="productivity"
android:hardwareAccelerated="true"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name=".ApplicationClass"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="app.freeairdrop.io.MainActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize|screenLayout"
android:resizeableActivity="false"
android:supportsPictureInPicture="false"
android:launchMode="singleInstance"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="freeairdrop.io" />
</intent-filter>
</activity>
</application>
</manifest>
Solution
Setting android:launchMode="singleInstance" to the activity will cause only one instance of that activity to run. Meaning when your activity has been opened once via deeplink that instance is running. When you open the second link you expect your UI to get updated but since the instance was still running the activity's onCreate does not get called, which is why you see the views from earlier link.( Another point is when singleInstance is set it won't allow any other activity in stack.)
You can get more info in the official docs .
Now, even though onCreate is never called but onResume and onNewIntent methods do get called when singleInstance is set. Though onNewIntent is usually mentioned to work with singleTop, from my experience it does get called on when any of the three single flags are set.
So you need to override either of the two and write the code to update your UI in these. Also note that onResume does get called when your activity is created the very first time after onCreate so if that's where you place the code you might want to optimize to avoid reloading the same thing twice and everytime onResume gets called like after returning from background. In which case onNewIntent() does seem like a better option.
Hope this was of some help to you.
Update: As requested, you can find the edited code below
package app.freeairdrop.io;
import...
public class MainActivity extends Activity{
private ProgressBar progressBar;
private WebView webView;
private SwipeRefreshLayout mySwipeRefreshLayout;
private boolean mShouldPause;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClientDemo());
webView.setWebChromeClient(new WebChromeClientDemo());
mySwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
mySwipeRefreshLayout.setRefreshing(false);
}
}
);
// call your method here to manage the intent
manageIntent(getIntent());
}
/* Simply moved your code which handles the intent to a common method
* Call this method from onCreate so that when first instance of activity gets created it handles it
* Similarly call it from onNewIntent to manage the new link you get
* You just need to pass the respective intents from the methods
*/
public void manageIntent(Intent intent) {
// ATTENTION: This was auto-generated to handle app links.
Intent appLinkIntent = intent;
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();
if (getIntent().getExtras() != null) {
if (appLinkData == null){
webView.loadUrl("https://freeairdrop.io/");
}else
webView.loadUrl(String.valueOf(appLinkData));
} else if (getIntent().getExtras() == null){
webView.loadUrl("https://freeairdrop.io/");
}
}
// override to get the new intent when this activity has an instance already running
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// again call the same method here with the new intent received
manageIntent(intent);
}
private class WebViewClientDemo extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toasty.error(getApplicationContext(), "No Internet, pull down to refresh when you're connected to internet", Toast.LENGTH_LONG, true).show();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
if (uri.getHost() != null && (url.startsWith("https://freeairdrop.io/") || url.startsWith("https://www.freeairdrop.io/"))) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
else {
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
// This method is used to detect back button
public void onBackPressed() {
if (this.webView.canGoBack()) {
this.webView.goBack();
return;
}
else {
// Let the system handle the back button
super.onBackPressed();
}
}
}
See if this works for you and let me know.
Answered By - ljk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.