I feel like the answer should be obvious but I can't find it. How do I reference a WebView in my main activity from Application class? Using a public static var to hold a reference to it in Activity class is causing a memory leak.
Current code (works, but causes memory leak).
Activity
public class Core extends AppCompatActivity {
public static WebView coreView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_core);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setPadding(0,80,0,0);
coreView = myWebView;
}
}
Application
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Core.coreView.loadUrl("http://www.google.com");
}
EDIT: Full App class.
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.setNotificationOpenedHandler(new MainNotificationOpenedHandler())
.setNotificationReceivedHandler(new MainNotificationReceivedHandler())
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
}
private class MainNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {
OSNotificationAction.ActionType actionType = result.action.type;
JSONObject data = result.notification.payload.additionalData;
String customKey;
Core.coreView.loadUrl(result.notification.payload.launchURL);
}
}
private class MainNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
@Override
public void notificationReceived(OSNotification notification) {
JSONObject data = notification.payload.additionalData;
}
}
}