Java Question how to pass "this"

raaiman

Member
Licensed User
Longtime User
B4X:
package com.example.TestMode;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.IQzone.postitial.Postitial;
public class MainActivity extends Activity {
    //some code ...
    @Override
    protected void onResume() {
      super.onResume();
      Postitial.initialize(this).getMonitor().onResumed(this);
    }
    @Override
    protected void onPause() {
      super.onPause();
      Postitial.initialize(this).getMonitor().onPaused(this);
    }
}


As code above, if i need to make a lib for B4A ,there is some function like
B4X:
public void Pause(A) {
      Postitial.initialize(A).getMonitor().onPaused(A);
    }


and in B4A ,i want to call it like
B4X:
Sub Activity_Pause (UserClosed As Boolean)
      test.Pause(B)
End Sub

now ,my question is what A and B should be?


thank you
 

raaiman

Member
Licensed User
Longtime User
Moved to the libraries developers forum.

1. Mark the class with @ActivityObject.
2. Add a BA parameter to the method and then you can use BA.activity.


i do it as you said,like

B4X:
@ActivityObject

public class testmod extends Activity {
   
    private BA ba;
   
    public String  initialize(anywheresoftware.b4a.BA _ba) throws Exception{
        this.ba = _ba;   
        Common.Log(_ba.className);
        if (_ba != null ){
        Common.Log(_ba.className);
        if (_ba.activity != null )
            Common.Log(_ba.activity.toString());
        else
            Common.Log("activity null");
        if (_ba.context != null)
            Common.Log(_ba.context.toString());
        else
            Common.Log("context null");
       
        Postitial.initialize(this.ba.activity);       
        }
        return _ba.className;
        }
   
}

The "Postitial.initialize" is "Postitial com.postitial.Postitial.initialize(Context arg0)".
Log _ba.context and _ba.activity is the same,b4a.example.main@416820c0 .
And i pass _ba.context or _ba.activity to Postitial.initialize(...), error comes
B4X:
b4a.example.main
b4a.example.main
b4a.example.main@416820c0
b4a.example.main@416820c0
main_activity_create (java line: 240)
java.lang.RuntimeException: <Postitial><2>, Postitial not initialized properly! Check the manifest configuration! context=android.app.Application@4161ddd8 appToken=null
    at com.postitial.Postitial.initialize(Unknown Source)
    at anywheresoftware.b4a.sample.smaato.initialize(smaato.java:47)
    at b4a.example.main._activity_create(main.java:240)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at b4a.example.main.afterFirstLayout(main.java:89)
    at b4a.example.main.access$100(main.java:16)
    at b4a.example.main$WaitForLayout.run(main.java:74)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:4624)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: <Postitial><2>, Postitial not initialized properly! Check the manifest configuration! context=android.app.Application@4161ddd8 appToken=null
 

raaiman

Member
Licensed User
Longtime User
Probably the context?
What is the signature of Postitial.initialize?
My guess will be you need to pass ba.context (which is passed automatically from B4A).


The "Postitial.initialize" is "Postitial com.postitial.Postitial.initialize(Context arg0)".
Log _ba.context and _ba.activity is the same,b4a.example.main@416820c0 , and where the difference is?
 

raaiman

Member
Licensed User
Longtime User
You also need to manually add this activity to the manifest file.
Something like this:
AddApplicationText(<activity android:name="com.postitial.Postitial"/>)

Depending on whatg the activity name is.
ba.context is indeed the same as ba.activity in this case.

This is however not the cause of this error.
'this' in your first code is equivalent to ba.activity (or ba.context).


Now ,I see the doc what i need to add to lib,
There is a statement, like this
"
Add Postitial SDK initialization to your Android project’s onCreate() method. Do this within your
"android.app.application", not within your Activity
"


So, can i Do THIS within My android.app.application.onCreate()???


thank you for helping me
 

raaiman

Member
Licensed User
Longtime User
It is possible. You will need to create a custom Application class and set it in the manifest editor as done here: http://www.b4x.com/forum/showthread.php?p=168344
It works. thanks.
And I try to set "Main Activity" ,by adding
B4X:
SetActivityAttribute(Main,android:name,"anywheresoftware.b4a.sample$ParseActivity")

ParseActivity like
B4X:
public static class ParseActivity extends Activity {

        protected void onResume() {
              super.onResume();
              BA.LogInfo("smaato onResume start");
              BA.LogInfo("smaato onResume end");
            }
        protected void onPause() {
              super.onPause();
              BA.LogInfo("smaato onPause start");
              BA.LogInfo("smaato onPause end");
            }
      }

But,when app run,nothing will appear.

shout i make "ParseActivity" full functions , like "\Objects\src\b4a\example\test\main.java" do?
 
Last edited:

raaiman

Member
Licensed User
Longtime User
You should not rename the main activity. You should instead use AddApplicationText to add the declaration of the new activity.
hm, But I want to add code to the " Main.onResume and onPause".


Now , I make my lib to a activity object, and use then Ba param to do this.

thank you very much,Erel. :)
 
Last edited:
Top