I have this simple Activity Module for testing onCreate and onNewIntent events and respective hooks.
B4X:
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
End Sub
Sub Class_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
#if java
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public void oncreate() {
Log.d("X", "oncreate");
}
public void onnewintent(Intent intent) {
Log.d("X", "onnewintent");
}
#End if
The intent filter for the activity is correct. The activity open up, but the events are not called. Unfortunately, I could not figure out why.
I have opened the generated java files and confirmed that they are being called on respective events.
Java:
processBA.runHook("oncreate", this, null);
What could be the reason for hooks not being reached?
(this might be a little too much to ask from ChatGPT, but...)
ChatGPT Says...
The issue you're encountering, where the hooks for oncreate and onnewintent are not being triggered as expected, seems to involve a potential misalignment or misunderstanding of the integration between the B4A (Basic4android) code and the custom Java code you are trying to use within the B4A environment.
Here are some steps and considerations to potentially resolve the issue:
Check Hook Integration:
Ensure that the hooks (' processBA.runHook("oncreate", this, null); ' ) in your Java code correspond to the correct lifecycle methods in Android. For instance, the hook for oncreate should ideally be placed within the onCreate method of your Java Activity class, and not in a separately defined method named oncreate. Make sure the capitalization and context are correctly implemented. In Android, the method is onCreate(Bundle savedInstanceState).
Java Method Naming and Override:
In Java, the Android lifecycle methods are case sensitive and should be annotated with @override to indicate that you're overriding a method from a superclass, like so:
Ensure these methods are correctly named and are overriding the Android Activity method.
Confirm Hook Definition in B4A:
Verify that the hooks oncreate and onnewintent are defined correctly within your B4A project. This involves checking whether the respective hooks are meant to execute specific code in B4A when called from Java.
Debugging Logs:
Add more logging both in Java and B4A to trace the flow of execution. This can help determine whether the methods in Java are being called and if the hooks in B4A are executed accordingly.
Review Activity Lifecycle:
Double-check the activity lifecycle to ensure that the lifecycle events you are hooking into are the correct points for what you intend to do. For example, if oncreate in your Java side is meant to run only once during the activity lifecycle, make sure it aligns with the B4A lifecycle expectations.
Permissions and Manifest Settings:
Occasionally, issues may arise from Android manifest settings or from permissions not being correctly set. Ensure that all necessary permissions are granted and that the manifest is correctly configured for your activity, especially if Intent objects are involved.
If after trying these steps the hooks still do not work, you might need to reconsider how the integration is set up or consult the B4A forums or documentation for more specific guidance regarding the use of Java hooks in B4A applications.
I already did all those suggestions.
Even the wrong code that ChatGPT suggested (I can not Override, because there is an onNewIntent on the generated code)
In the logs I can see the wincomingintent activity being created and resumed as should be.
The generated file has the calling for the oncreate and onnewintent event. Even there was no onNewIntent event, I can imagine at least oncreate hook should be called, because the onCreate happened.
Java:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mostCurrent = this;
if (processBA == null) {
processBA = new anywheresoftware.b4a.ShellBA(this.getApplicationContext(), null, null, "b4a.example", "b4a.example.wincomingintent");
processBA.loadHtSubs(this.getClass());
float deviceScale = getApplicationContext().getResources().getDisplayMetrics().density;
BALayout.setDeviceScale(deviceScale);
}
else if (previousOne != null) {
Activity p = previousOne.get();
if (p != null && p != this) {
BA.LogInfo("Killing previous instance (wincomingintent).");
p.finish();
}
}
processBA.setActivityPaused(true);
processBA.runHook("oncreate", this, null);
if (!includeTitle) {
this.getWindow().requestFeature(android.view.Window.FEATURE_NO_TITLE);
}
Java:
@Override
public void onNewIntent(android.content.Intent intent) {
super.onNewIntent(intent);
this.setIntent(intent);
processBA.runHook("onnewintent", this, new Object[] {intent});
}
And the hook methods:
Java:
public void oncreate() {
BA.LogInfo("oncreate");
}
public void onnewintent(Intent intent) {
BA.LogInfo("onnewintent");
}