Skip to content

Commit b22b98d

Browse files
committed
Use typed AndroidX fragment stubs
1 parent 41f0560 commit b22b98d

4 files changed

Lines changed: 115 additions & 22 deletions

File tree

jme3-android/src/androidx-stubs/java/androidx/fragment/app/FragmentActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@
4040
* application. This class is excluded from jme3-android artifacts.</p>
4141
*/
4242
public class FragmentActivity extends Activity {
43+
44+
public FragmentManager getSupportFragmentManager() {
45+
return null;
46+
}
4347
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2009-2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package androidx.fragment.app;
33+
34+
/**
35+
* Compile-time stub for the AndroidX FragmentManager API.
36+
*
37+
* <p>The real AndroidX Fragment dependency must be supplied by the Android
38+
* application. This class is excluded from jme3-android artifacts.</p>
39+
*/
40+
public class FragmentManager {
41+
42+
public Fragment findFragmentByTag(String tag) {
43+
return null;
44+
}
45+
46+
public FragmentTransaction beginTransaction() {
47+
return null;
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2009-2026 jMonkeyEngine
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package androidx.fragment.app;
33+
34+
/**
35+
* Compile-time stub for the AndroidX FragmentTransaction API.
36+
*
37+
* <p>The real AndroidX Fragment dependency must be supplied by the Android
38+
* application. This class is excluded from jme3-android artifacts.</p>
39+
*/
40+
public class FragmentTransaction {
41+
42+
public FragmentTransaction replace(int containerViewId, Fragment fragment, String tag) {
43+
return this;
44+
}
45+
46+
public int commit() {
47+
return 0;
48+
}
49+
}

jme3-android/src/main/java/com/jme3/app/AndroidHarness.java

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
import android.view.WindowManager;
4545
import android.widget.FrameLayout;
4646
import android.widget.ImageView;
47+
import androidx.fragment.app.Fragment;
4748
import androidx.fragment.app.FragmentActivity;
49+
import androidx.fragment.app.FragmentManager;
4850
import com.jme3.audio.AudioRenderer;
4951
import com.jme3.input.JoyInput;
5052
import com.jme3.input.TouchInput;
@@ -58,7 +60,6 @@
5860
import com.jme3.system.android.OGLESContext;
5961
import java.io.PrintWriter;
6062
import java.io.StringWriter;
61-
import java.lang.reflect.Method;
6263
import java.util.logging.Level;
6364
import java.util.logging.Logger;
6465

@@ -112,15 +113,14 @@ public class AndroidHarness extends FragmentActivity
112113
protected ImageView splashImageView;
113114
protected FrameLayout frameLayout;
114115

115-
private HarnessFragment fragment;
116116
private boolean firstDrawFrame = true;
117117

118118
@Override
119119
protected void onCreate(Bundle savedInstanceState) {
120120
super.onCreate(savedInstanceState);
121121
configureWindow();
122122

123-
fragment = attachFragment();
123+
HarnessFragment fragment = attachFragment();
124124
fragment.setFinishOnAppStop(finishOnAppStop);
125125
}
126126

@@ -139,26 +139,17 @@ public Application getJmeApplication() {
139139
}
140140

141141
private HarnessFragment attachFragment() {
142-
try {
143-
Method getSupportFragmentManager = getClass().getMethod("getSupportFragmentManager");
144-
Object fragmentManager = getSupportFragmentManager.invoke(this);
145-
Object existingFragment = fragmentManager.getClass()
146-
.getMethod("findFragmentByTag", String.class)
147-
.invoke(fragmentManager, HARNESS_FRAGMENT_TAG);
148-
if (existingFragment instanceof HarnessFragment) {
149-
return (HarnessFragment) existingFragment;
150-
}
151-
152-
HarnessFragment newFragment = new HarnessFragment();
153-
Object transaction = fragmentManager.getClass().getMethod("beginTransaction").invoke(fragmentManager);
154-
transaction = transaction.getClass()
155-
.getMethod("replace", int.class, androidx.fragment.app.Fragment.class, String.class)
156-
.invoke(transaction, android.R.id.content, newFragment, HARNESS_FRAGMENT_TAG);
157-
transaction.getClass().getMethod("commit").invoke(transaction);
158-
return newFragment;
159-
} catch (ReflectiveOperationException exception) {
160-
throw new IllegalStateException("Unable to attach AndroidHarnessFragment", exception);
142+
FragmentManager fragmentManager = getSupportFragmentManager();
143+
Fragment existingFragment = fragmentManager.findFragmentByTag(HARNESS_FRAGMENT_TAG);
144+
if (existingFragment instanceof HarnessFragment) {
145+
return (HarnessFragment) existingFragment;
161146
}
147+
148+
HarnessFragment newFragment = new HarnessFragment();
149+
fragmentManager.beginTransaction()
150+
.replace(android.R.id.content, newFragment, HARNESS_FRAGMENT_TAG)
151+
.commit();
152+
return newFragment;
162153
}
163154

164155
@Override

0 commit comments

Comments
 (0)