Android Question Inline java code scope

DonManfred

Expert
Licensed User
Longtime User
Nativeme is a process gobal variable. You can access it from everywhere.

Methods added to Activity or Service modules will be executed with the component context ('this' will be the activity or service instance).
So i guess you are not able to run code from an activity which is in background or destroyed
 
Last edited:
Upvote 0

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
If you don't need an activity context then you can move the inline code to a code module and then access it from all other modules.

In most cases it is better to expose a B4A sub that will call the native method.



I wrote these codes and would like to make the methods available for all activities without rewriting the java .

B4X:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <plurals name="message">
        <item quantity="one">You have one new message</item>
        <item quantity="other">You have %d new messages</item>
    </plurals>
</resources>

'JAVA

#if Java

public int getResourceId(String type, String name)
    {
        return BA.applicationContext.getResources().getIdentifier(name,    type, BA.packageName);
    }

    public String FormatPlural(int id, int count) {

        return BA.applicationContext.getResources().getQuantityString(id,  count, count);
    }

#End If

'B4A
Sub Activity_Create(FirstTime As Boolean)

   Private NativeMe As JavaObject
   NativeMe.InitializeContext
   Dim id As Int = NativeMe.RunMethod("getResourceId", Array("plurals","message"))

   Dim s As String = NativeMe.RunMethod("FormatPlural", Array(id,1))
   Log(s)

   Dim s As String = NativeMe.RunMethod("FormatPlural", Array(id,2))
   Log(s)

End Sub

'RESULTS
'You have one new message
'You have 2 new messages
 
Upvote 0
Top