Skip to content

Commit 23a06e2

Browse files
Fix multiple issues
1 parent c0faed6 commit 23a06e2

File tree

4 files changed

+34
-56
lines changed

4 files changed

+34
-56
lines changed

README.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ 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.0.0
25+
- Merge changes to fix multiple issues (thanks https://github.com/mediathand)
26+
2427
## 1.0.0
2528
- Migrate to androidx
2629

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="1.0.0">
2+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="cordova-plugin-android-fragmentactivity" version="2.0.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: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ Licensed to the Apache Software Foundation (ASF) under one
2121

2222
/** extends CordovaActivity */
2323

24-
import android.app.FragmentTransaction;
2524
import android.content.Intent;
2625
import android.os.Bundle;
2726
import androidx.fragment.app.FragmentActivity;
28-
// import android.support.v4.app.FragmentActivity;
27+
import androidx.fragment.app.FragmentTransaction;
2928

3029
public class MainActivity extends FragmentActivity
3130
{
@@ -40,56 +39,11 @@ public void onCreate(Bundle savedInstanceState)
4039
super.onCreate(savedInstanceState);
4140

4241
currentFragment = new uk.co.reallysmall.cordova.plugin.fragment.CordovaFragment();
43-
FragmentTransaction ft = getFragmentManager().beginTransaction();
42+
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
4443
ft.add(android.R.id.content, currentFragment);
4544
ft.commit();
4645
}
4746

48-
/**
49-
* Called when the system is about to start resuming a previous activity.
50-
*/
51-
@Override
52-
protected void onPause() {
53-
super.onPause();
54-
currentFragment.onPause();
55-
}
56-
57-
/**
58-
* Called when the activity will start interacting with the user.
59-
*/
60-
@Override
61-
protected void onResume() {
62-
super.onResume();
63-
currentFragment.onResume();
64-
}
65-
66-
/**
67-
* Called when the activity is no longer visible to the user.
68-
*/
69-
@Override
70-
protected void onStop() {
71-
super.onStop();
72-
currentFragment.onStop();
73-
}
74-
75-
/**
76-
* Called when the activity is becoming visible to the user.
77-
*/
78-
@Override
79-
protected void onStart() {
80-
super.onStart();
81-
currentFragment.onStart();
82-
}
83-
84-
/**
85-
* The final call you receive before your activity is destroyed.
86-
*/
87-
@Override
88-
public void onDestroy() {
89-
super.onDestroy();
90-
currentFragment.onDestroy();
91-
}
92-
9347
@Override
9448
public void onActivityResult(int requestCode, int resultCode, Intent data) {
9549
super.onActivityResult(requestCode,resultCode,data);

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2626
import android.media.AudioManager;
2727
import android.net.http.SslError;
2828
import android.os.Bundle;
29-
import android.app.Fragment;
29+
import android.os.Handler;
3030
import android.util.Log;
3131
import android.view.LayoutInflater;
3232
import android.view.Menu;
@@ -40,6 +40,8 @@ Licensed to the Apache Software Foundation (ASF) under one
4040
import android.webkit.WebViewClient;
4141
import android.widget.FrameLayout;
4242

43+
import androidx.fragment.app.Fragment;
44+
4345
import org.apache.cordova.BuildConfig;
4446
import org.apache.cordova.ConfigXmlParser;
4547
import org.apache.cordova.CordovaInterfaceImpl;
@@ -96,7 +98,8 @@ public class CordovaFragment extends Fragment {
9698
// The webview for our app
9799
protected CordovaWebView appView;
98100

99-
101+
final Handler mHandler = new Handler();
102+
100103
public CordovaWebView getAppView() {
101104
return appView;
102105
}
@@ -112,24 +115,30 @@ public CordovaWebView getAppView() {
112115
protected ArrayList<PluginEntry> pluginEntries;
113116
protected CordovaInterfaceImpl cordovaInterface;
114117

115-
private View contentView;
118+
private FrameLayout contentView;
116119

117120
public View getContentView() {
118121
return contentView;
119122
}
120123

121124
public void setContentView(View contentView) {
122-
FrameLayout frame = new FrameLayout(this.getActivity().getBaseContext());
123-
frame.addView(contentView);
124-
this.contentView = frame;
125+
this.contentView.removeAllViews();
126+
this.contentView.addView(contentView);
125127
}
126128

127129
@Override
128130
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
129131
if(contentView == null){
130-
init();
132+
this.contentView = new FrameLayout(this.getActivity().getBaseContext());
131133
}
132134
loadUrl(launchUrl);
135+
136+
mHandler.post(new Runnable() {
137+
public void run() {
138+
loadUrl(launchUrl);
139+
}
140+
});
141+
133142
return contentView;
134143
}
135144

@@ -270,6 +279,18 @@ public void loadUrl(String url) {
270279
appView.loadUrlIntoView(url, true);
271280
}
272281

282+
@Override
283+
public void onPause() {
284+
super.onPause();
285+
LOG.d(TAG, "Paused the activity.");
286+
287+
if (this.appView == null) {
288+
return;
289+
}
290+
291+
this.appView.handlePause(this.keepRunning);
292+
}
293+
273294
/**
274295
* Called when the activity will start interacting with the user.
275296
*/

0 commit comments

Comments
 (0)