Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit 5657132

Browse files
cmarcelkhuningxin
authored andcommitted
CB-7674 move preference activation back into onCreate()
The preference creation actually needs to be before super.onCreate(savedInstance) in order to avoid the exception "requestFeature() must be called before adding content". Also ran into an issue in the native tests "Whitelist" and "User WebView/Client/Chrome" where it would throw an exception that the CordovaWebView appView already had a parent and needed to be removed from that parent before the invocation to root.addView(appView). So I conditionally remove the wrong parent. Also made a change to the native tests so the menus test would work. I also put super.init() back into the template, though invoking it is optional as loadUrl will call it automatically if needed. (cherry picked from commit 90766ae0faa6594a6f4d6b9d61d66af8e3600b8e) BUG=XWALK-3171 Conflicts: framework/src/org/apache/cordova/CordovaActivity.java
1 parent b67d26e commit 5657132

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

bin/templates/project/Activity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class __ACTIVITY__ extends CordovaActivity
2828
public void onCreate(Bundle savedInstanceState)
2929
{
3030
super.onCreate(savedInstanceState);
31+
super.init();
3132
// Set by <content src="index.html" /> in config.xml
3233
loadUrl(launchUrl);
3334
}

framework/src/org/apache/cordova/CordovaActivity.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Licensed to the Apache Software Foundation (ASF) under one
5454
import android.view.OrientationEventListener;
5555
import android.view.View;
5656
import android.view.ViewGroup;
57+
import android.view.ViewParent;
5758
import android.view.Window;
5859
import android.view.WindowManager;
5960
import android.webkit.ValueCallback;
@@ -77,6 +78,7 @@ Licensed to the Apache Software Foundation (ASF) under one
7778
* &#64;Override
7879
* public void onCreate(Bundle savedInstanceState) {
7980
* super.onCreate(savedInstanceState);
81+
* super.init();
8082
* // Load your application
8183
* loadUrl(launchUrl);
8284
* }
@@ -205,14 +207,32 @@ public void clearAuthenticationTokens() {
205207
public void onCreate(Bundle savedInstanceState) {
206208
LOG.i(TAG, "Apache Cordova native platform version " + CordovaWebView.CORDOVA_VERSION + " is starting");
207209
LOG.d(TAG, "CordovaActivity.onCreate()");
210+
211+
// need to activate preferences before super.onCreate to avoid "requestFeature() must be called before adding content" exception
212+
loadConfig();
213+
if(!preferences.getBoolean("ShowTitle", false))
214+
{
215+
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
216+
}
217+
218+
if(preferences.getBoolean("SetFullscreen", false))
219+
{
220+
Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version.");
221+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
222+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
223+
} else if (preferences.getBoolean("Fullscreen", false)) {
224+
toggleFullscreen(getWindow());
225+
} else {
226+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
227+
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
228+
}
229+
208230
super.onCreate(savedInstanceState);
209231

210232
if(savedInstanceState != null)
211233
{
212234
initCallbackClass = savedInstanceState.getString("callbackClass");
213235
}
214-
215-
loadConfig();
216236
}
217237

218238
@SuppressWarnings("deprecation")
@@ -232,7 +252,9 @@ protected void loadConfig() {
232252
@SuppressWarnings("deprecation")
233253
protected void createViews() {
234254
// This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket!
235-
// This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket!
255+
256+
LOG.d(TAG, "CordovaActivity.createViews()");
257+
236258
Display display = getWindowManager().getDefaultDisplay();
237259
int width = display.getWidth();
238260
int height = display.getHeight();
@@ -250,6 +272,14 @@ protected void createViews() {
250272

251273
// Add web view but make it invisible while loading URL
252274
appView.setVisibility(View.INVISIBLE);
275+
276+
// need to remove appView from any existing parent before invoking root.addView(appView)
277+
ViewParent parent = appView.getParent();
278+
if ((parent != null) && (parent != root)) {
279+
LOG.d(TAG, "removing appView from existing parent");
280+
ViewGroup parentGroup = (ViewGroup) parent;
281+
parentGroup.removeView(appView);
282+
}
253283
root.addView((View) appView);
254284
setContentView(root);
255285

@@ -328,23 +358,6 @@ public void init() {
328358
public void init(CordovaWebView webView, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient) {
329359
LOG.d(TAG, "CordovaActivity.init()");
330360

331-
if(!preferences.getBoolean("ShowTitle", false))
332-
{
333-
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
334-
}
335-
336-
if(preferences.getBoolean("SetFullscreen", false))
337-
{
338-
Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version.");
339-
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
340-
WindowManager.LayoutParams.FLAG_FULLSCREEN);
341-
} else if (preferences.getBoolean("Fullscreen", false)) {
342-
toggleFullscreen(getWindow());
343-
} else {
344-
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
345-
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
346-
}
347-
348361
appView = webView != null ? webView : makeWebView();
349362
if (appView.pluginManager == null) {
350363
appView.init(this, webViewClient != null ? webViewClient : makeWebViewClient(appView),

test/res/xml/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<preference name="loglevel" value="DEBUG" />
3535
<preference name="useBrowserHistory" value="true" />
3636
<preference name="exit-on-suspend" value="false" />
37+
<preference name="showTitle" value="true" />
3738
<feature name="Activity">
3839
<param name="android-package" value="org.apache.cordova.test.ActivityPlugin" />
3940
</feature>

test/src/org/apache/cordova/test/menus.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public class menus extends CordovaActivity {
3232
@Override
3333
public void onCreate(Bundle savedInstanceState) {
3434
super.onCreate(savedInstanceState);
35-
// need the title to be shown for the options menu to be visible
36-
preferences.set("showTitle", true);
35+
// need the title to be shown (config.xml) for the options menu to be visible
3736
super.init();
3837
super.registerForContextMenu(super.appView);
3938
super.loadUrl("file:///android_asset/www/menus/index.html");

0 commit comments

Comments
 (0)