Java Question Extending Activity class

thedesolatesoul

Expert
Licensed User
Longtime User
Are we able to extend an activity to a B4A activity?

Basically I want to emulate:

B4X:
public class MyNewActivity extends SDKActivities.Activity {
    // Your activity code...
}

And then use this Activity as usual in B4A (not an activity in a library).

As far as I know we have not supported this yet.
 

barx

Well-Known Member
Licensed User
Longtime User
Nope, been there asked that...
 

thedesolatesoul

Expert
Licensed User
Longtime User
Hi Erel,
What the preference activity is doing it creates the activity inside the library, and then returns an intent to launch it. (Later there is no control on the activity, i.e. no user code).
I guess I could raise the events from the activity into a service, but this means the activity is fairly static.

I am not sure how B4A activities are defined. I think once you add the module, the code for defining the activity is added at compile time directly to the java source.
Is it possible to add a 'custom module' in the menu, which then allows us to subclass B4AActivity or service?
 

thedesolatesoul

Expert
Licensed User
Longtime User
You will not be able to use layout files with this library. Can't you use one or two customizable layouts (like done in ListView)?
I could but then the library will not be re-usable anymore.
The idea of that SDK is to take *any* activity, and with minimal changes, it can become floating/sidebar.
 

vpires

Member
Licensed User
Longtime User
@ thedesolatesoul : sorry to hijack your thread....
@Erel : yep. Thats what i've done.

B4X:
@Hide
public static class PoolFishActivity extends Activity implements com.pollfish.interfaces.PollfishSurveyReceivedListener,
                                    PollfishSurveyNotAvailableListener,
                                    PollfishSurveyCompletedListener,
                                    PollfishOpenedListener,
                                    PollfishClosedListener {
                      
   
    public BA Ba_;
    private String event_;
                      
    @Override
    public void onPollfishSurveyReceived() {
        Ba_.raiseEvent(Ba_, event_ + "_onpollfishsurveyreceived");
        Log.d("B4A", "Survey Received");
    }
 

thedesolatesoul

Expert
Licensed User
Longtime User
There is actually a way to change the B4A activity parent class. It is not exposed as I wasn't sure that there is a valid use case for it.

It is worth a try.
The #Extends attribute allows you to change the parent class. The default value is Activity.

B4X:
#Extends: SDKActivities.Activity
Erel this is truly BRILLIANT!!! Exactly what I needed.
You are the star of the show!!! I am surprised you never told us before :)
Thank you.

And now, it almost works like I wanted:
B4X:
public class main extends TooleapActivities.Activity implements B4AActivity{

However, it complains about TooleapActivities.Activity : package TooleapActivities does not exist
I guess the import doesnt exist in main.java,
so I tried to create a wrapper that only uses the dependson code to refer to the library but no luck. I tried a couple of other things but so far I have not been able to get it to import these packages.

@ thedesolatesoul : sorry to hijack your thread....
@Erel : yep. Thats what i've done.

B4X:
@Hide
public static class PoolFishActivity extends Activity implements com.pollfish.interfaces.PollfishSurveyReceivedListener,
                                    PollfishSurveyNotAvailableListener,
                                    PollfishSurveyCompletedListener,
                                    PollfishOpenedListener,
                                    PollfishClosedListener {
                     
  
    public BA Ba_;
    private String event_;
                     
    @Override
    public void onPollfishSurveyReceived() {
        Ba_.raiseEvent(Ba_, event_ + "_onpollfishsurveyreceived");
        Log.d("B4A", "Survey Received");
    }
Probably better to start a new thread in case you need to search for it later.
But must I say, so you are the guy writing the wrapper!!! http://www.b4x.com/android/forum/threads/pollfish-sdk-wrapper.40326/
Also, you never mentioned what isnt working in your class. It looks great except you have not assigned BA and event so I dont know how it will raise the event.
 

warwound

Expert
Licensed User
Longtime User
Shall i start a new thread or post here...

Trying out the #Extends attribute i get this compilation error:

Parsing code. 0.00
Compiling code. 0.05
Compiling layouts code. 0.02
Generating R file. 0.06
Compiling generated Java code. Error
javac 1.8.0_05
src\uk\co\martinpearman\b4a\analyticsmanagerdemo\main.java:30: error: method does not override or implement a method from a supertype
@Override
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

Lines 30 and 31 in the compiled b4a main.java read:

B4X:
@Override
	public void onCreate(Bundle savedInstanceState) {

My library sub classes Activity to enable Flurry analytics:

B4X:
package uk.co.martinpearman.b4a.analytics;

import com.flurry.android.FlurryAgent;

import android.app.Activity;
import android.content.res.Resources;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.Permissions;

@DependsOn(values={
		"flurryAnalytics_3.4.0"
	})
@Hide
@Permissions(values={
		"android.permission.ACCESS_NETWORK_STATE",
		"android.permission.INTERNET"
	})
public class AnalyticsActivity extends Activity{

	public static boolean FLURRY_ANALYTICS_ENABLED=false;
	public static String FLURRY_API_KEY=null;

	static{
		Resources resources=BA.applicationContext.getResources();
		int resourceId=resources.getIdentifier("flurryApiKey", "string", BA.packageName);
		if(resourceId==0){
			FLURRY_ANALYTICS_ENABLED=false;
			FLURRY_API_KEY=null;
		} else {
			FLURRY_ANALYTICS_ENABLED=true;
			FLURRY_API_KEY=resources.getString(resourceId);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		if(FLURRY_ANALYTICS_ENABLED){
			FlurryAgent.onStartSession(this, FLURRY_API_KEY);
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		if(FLURRY_ANALYTICS_ENABLED){
			FlurryAgent.onEndSession(this);
		}
	}
}

And my b4a Main Activity:

B4X:
#Region  Project Attributes 
	#ApplicationLabel: AnalyticsManager
	#VersionCode: 1
	#VersionName: 1
	'SupportedOrientations possible values: unspecified, landscape or portrait.
	#SupportedOrientations: unspecified
	#CanInstallToExternalStorage: False
	#AdditionalRes: C:\path\to\AnalyticsManager\res, uk.co.martinpearman.b4a.analyticsmanagerdemo
	#Extends: uk.co.martinpearman.b4a.analytics.AnalyticsManager
#End Region

#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: True
#End Region

Sub Process_Globals
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
	'	check out the Manifest Editor to see modifications made for the demo
	'	check out the res\values\analytics_manager.xml file where Flurry api key is defined
	
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

I tried implementing B4AActivity in my AnalyticsActivity and also commenting out it's static constructor.
Also tried overriding onCreate in my AnalyticsActivity - just called super.onCreate(savedInstanceState) here.
No matter what i do it fails to compile with the same error.
Compiling using android API 17 for both B4A and Eclipse.
Eclipse is set to use java complinace level 1.6.

Any ideas?

Thanks.

Martin.
 
Top