I need to have two separate activities in an Android app. As far as I understand this is done with having multiple <activity> tags in the <application> tag. Many things online indicate that you use something like this code:
Intent intent = new Intent(this, mynativeactivity.class);
startActivity(intent);
However, the android:name (and by extension, class) of both Native Activities is android.app.NativeActivity. They are just in separate .so files. Even if I use the JNI.
Is there a way of switching activities based on a string name, alias or label of the specific android activity?
My AndroidManifest.xml looks something like this:
<application android:debuggable="true" android:hasCode="false" android:label="cnfgtest" tools:replace="android:icon,android:theme,android:allowBackup,label" android:icon="@mipmap/icon">
<activity android:configChanges="keyboardHidden|orientation" android:label="app_i_want_to_launch" android:name="android.app.NativeActivity">
<meta-data android:name="android.app.lib_name" android:value="app_i_want_to_launch"/>
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:configChanges="keyboardHidden|orientation" android:label="app_that_currently_launches" android:name="android.app.NativeActivity">
<meta-data android:name="android.app.lib_name" android:value="app_that_currently_launches"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
EDIT: Within this build environment, there's really no way to build any additional Java code. All solutions would need to be with just NativeActivity.
EDIT2: To elaborate on my finally executed solution. I ended up compiling a separate Java file outside my tree and included the classes.dex inside my project and let the build system continue as expected. This allowed me to make separate class that was also exactly a NativeActivity but by a different name. I.e.
# javac -source 1.7 -target 1.7 -d bin/org/yourorg/cnfgtest/ -classpath ${ANDROID_HOME}/platforms/android-30/android.jar -sourcepath src src/org/yourorg/cnfgtest/MyOtherNativeActivity.java
# ~/android-sdk/build-tools/30.0.2/dx --output=makecapk/ --dex ./bin #makecapk is the root of my project.
Then use each class to define a new native activity.
Also, I was able to make use of parts of @dalmif 's answers below.
Create another class and extend NativeActivity like this
now you can put it in
AndroidManifest.xml.finally just
startActivitywith this new class.Update
In general, you have two options for having a
NativeActivityfor each native library.NativeActivitydeclaration with modifiable<meta-data>tag. (In cases where you don't want to create Java files)the first option is explained above, but about the second, you can use this Application class
and declare this application class in your manifest
now you can simply use this code to set the lib name (
<meta-data android:name="android.app.lib_name" ...>) in your code instead of AndroidManifest.xml