Skip to content

Commit 20bd673

Browse files
Add fixes from main cordova repo and handle onNewIntent and existing fragment
1 parent 8bb7db2 commit 20bd673

File tree

5 files changed

+107
-19
lines changed

5 files changed

+107
-19
lines changed

README.MD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ AndroidManifest.xml is automatically updated to use the new MainActivity.
2121
Based on cordova-android-fragments (https://github.com/rajivnarayana/CordovaFragments)
2222

2323
# History
24+
## 2.2.0
25+
- Add support for onNewIntent
26+
- Apply some fixes from main cordova repo
27+
28+
## 2.1.0
29+
- Add fix to ensure only root task runs
30+
2431
## 2.0.0
2532
- Merge changes to fix multiple issues (thanks https://github.com/mediathand)
2633

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cordova-plugin-android-fragmentactivity",
3-
"version": "2.0.0",
3+
"version": "2.2.0",
44
"description": "An android plugin that provides a replacement activity to the default activity to start a cordova application with MainActivity as a Fragment Activity. Useful when you want to add native views on top of cordova webview.",
55
"cordova": {
66
"id": "cordova-plugin-android-fragmentactivity",

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-android-fragmentactivity" version="2.0.0">
2+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-android-fragmentactivity" version="2.2.0">
33

44
<name>Cordova Android FragmentActivity Plugin</name>
55
<description>An android plugin that provides a replacement activity to the default activity to start a cordova application with MainActivity as a Fragment Activity. Useful when you want to add native views on top of cordova webview.</description>

src/android/MainActivity.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ Licensed to the Apache Software Foundation (ASF) under one
2121

2222
/** extends CordovaActivity */
2323

24+
import android.annotation.SuppressLint;
2425
import android.content.Intent;
2526
import android.os.Bundle;
27+
import android.view.View;
28+
2629
import androidx.fragment.app.FragmentActivity;
30+
import androidx.fragment.app.FragmentManager;
2731
import androidx.fragment.app.FragmentTransaction;
2832

2933
public class MainActivity extends FragmentActivity
@@ -38,10 +42,47 @@ public void onCreate(Bundle savedInstanceState)
3842

3943
super.onCreate(savedInstanceState);
4044

41-
currentFragment = new uk.co.reallysmall.cordova.plugin.fragment.CordovaFragment();
42-
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
43-
ft.add(android.R.id.content, currentFragment);
44-
ft.commit();
45+
if (!this.isTaskRoot()) {
46+
finish();
47+
return;
48+
}
49+
50+
FragmentManager fm = getSupportFragmentManager();
51+
52+
currentFragment = (CordovaFragment) fm.findFragmentByTag("${mypackage}");
53+
54+
if (currentFragment == null) {
55+
currentFragment = new uk.co.reallysmall.cordova.plugin.fragment.CordovaFragment();
56+
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
57+
ft.add(android.R.id.content, currentFragment,${mypackage});
58+
ft.commit();
59+
}
60+
}
61+
62+
protected void onSaveInstanceState(Bundle outState) {
63+
currentFragment.onSaveInstanceState(outState);
64+
super.onSaveInstanceState(outState);
65+
}
66+
67+
/**
68+
* Called when the activity receives a new intent
69+
*/
70+
@Override
71+
protected void onNewIntent(Intent intent) {
72+
super.onNewIntent(intent);
73+
74+
currentFragment.onNewIntent(intent);
75+
}
76+
77+
/**
78+
* Called when view focus is changed
79+
*/
80+
@SuppressLint("InlinedApi")
81+
@Override
82+
public void onWindowFocusChanged(boolean hasFocus) {
83+
super.onWindowFocusChanged(hasFocus);
84+
85+
currentFragment.onWindowFocusChanged(hasFocus);
4586
}
4687

4788
@Override

src/android/uk/co/reallysmall/cordova/plugin/fragment/CordovaFragment.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Licensed to the Apache Software Foundation (ASF) under one
4343
import androidx.fragment.app.Fragment;
4444

4545
import org.apache.cordova.BuildConfig;
46+
import org.apache.cordova.Config;
4647
import org.apache.cordova.ConfigXmlParser;
4748
import org.apache.cordova.CordovaInterfaceImpl;
4849
import org.apache.cordova.CordovaPreferences;
@@ -109,6 +110,9 @@ public CordovaWebView getAppView() {
109110
// when another application (activity) is started.
110111
protected boolean keepRunning = true;
111112

113+
// Flag to keep immersive mode if set to fullscreen
114+
protected boolean immersiveMode;
115+
112116
// Read from config.xml:
113117
protected CordovaPreferences preferences;
114118
protected String launchUrl;
@@ -170,14 +174,17 @@ public void onCreate(Bundle savedInstanceState) {
170174
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
171175
WindowManager.LayoutParams.FLAG_FULLSCREEN);
172176
} else if (preferences.getBoolean("Fullscreen", false)) {
173-
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
174-
WindowManager.LayoutParams.FLAG_FULLSCREEN);
177+
if (!preferences.getBoolean("FullscreenNotImmersive", false)) {
178+
immersiveMode = true;
179+
} else {
180+
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
181+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
182+
}
175183
} else {
176184
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
177185
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
178186
}
179187

180-
181188
super.onCreate(savedInstanceState);
182189

183190
cordovaInterface = makeCordovaInterface();
@@ -219,7 +226,7 @@ protected void loadConfig() {
219226
preferences.setPreferencesBundle(getActivity().getIntent().getExtras());
220227
launchUrl = parser.getLaunchUrl();
221228
pluginEntries = parser.getPluginEntries();
222-
// Config.parser = parser;
229+
// Config.parser = parser;
223230
}
224231

225232
//Suppressing warnings in AndroidStudio
@@ -234,9 +241,14 @@ protected void createViews() {
234241
setContentView(appView.getView());
235242

236243
if (preferences.contains("BackgroundColor")) {
237-
int backgroundColor = preferences.getInteger("BackgroundColor", Color.BLACK);
238-
// Background of activity:
239-
appView.getView().setBackgroundColor(backgroundColor);
244+
try {
245+
int backgroundColor = preferences.getInteger("BackgroundColor", Color.BLACK);
246+
// Background of activity:
247+
appView.getView().setBackgroundColor(backgroundColor);
248+
}
249+
catch (NumberFormatException e){
250+
e.printStackTrace();
251+
}
240252
}
241253

242254
appView.getView().requestFocusFromTouch();
@@ -284,11 +296,21 @@ public void onPause() {
284296
super.onPause();
285297
LOG.d(TAG, "Paused the activity.");
286298

287-
if (this.appView == null) {
288-
return;
299+
if (this.appView != null) {
300+
// CB-9382 If there is an activity that started for result and main activity is waiting for callback
301+
// result, we shoudn't stop WebView Javascript timers, as activity for result might be using them
302+
boolean keepRunning = this.keepRunning;// || this.cordovaInterface.activityResultCallback != null;
303+
this.appView.handlePause(keepRunning);
289304
}
305+
}
290306

291-
this.appView.handlePause(this.keepRunning);
307+
/**
308+
* Called when the activity receives a new intent
309+
*/
310+
public void onNewIntent(Intent intent) {
311+
//Forward to plugins
312+
if (this.appView != null)
313+
this.appView.onNewIntent(intent);
292314
}
293315

294316
/**
@@ -302,9 +324,11 @@ public void onResume() {
302324
if (this.appView == null) {
303325
return;
304326
}
305-
// Force window to have focus, so application always
306-
// receive user input. Workaround for some devices (Samsung Galaxy Note 3 at least)
307-
this.getActivity().getWindow().getDecorView().requestFocus();
327+
if (!getActivity().getWindow().getDecorView().hasFocus()) {
328+
// Force window to have focus, so application always
329+
// receive user input. Workaround for some devices (Samsung Galaxy Note 3 at least)
330+
getActivity().getWindow().getDecorView().requestFocus();
331+
}
308332

309333
this.appView.handleResume(this.keepRunning);
310334
}
@@ -352,6 +376,22 @@ public void onDestroy() {
352376
}
353377
}
354378

379+
/**
380+
* Called when view focus is changed
381+
*/
382+
public void onWindowFocusChanged(boolean hasFocus) {
383+
if (hasFocus && immersiveMode) {
384+
final int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
385+
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
386+
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
387+
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
388+
| View.SYSTEM_UI_FLAG_FULLSCREEN
389+
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
390+
391+
getActivity().getWindow().getDecorView().setSystemUiVisibility(uiOptions);
392+
}
393+
}
394+
355395
@Override
356396
public void startActivityForResult(Intent intent, int requestCode) {
357397
// Capture requestCode here so that it is captured in the setActivityResultCallback() case.

0 commit comments

Comments
 (0)