# Java QuestionExtending Activity class

#### thedesolatesoul

##### Expert
Are we able to extend an activity to a B4A activity?

Basically I want to emulate:

B4X:
public class MyNewActivity extends SDKActivities.Activity {
}

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.

#### Erel

##### B4X founder
Staff member
You can create libraries that subclass Activitiy (not one of the B4A activities). However it is more complicated. You can see PreferenceActivity library code for one example. In other cases it will require using a service to interact with the activity.

#### thedesolatesoul

##### Expert
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?

#### Erel

##### B4X founder
Staff member
Letting the activity interact with the service is the correct way.

#### thedesolatesoul

##### Expert
Yes, but how will I raise events for lets say buttons, edit text that are loaded from the layout file?
Will the service need a context or reference for these?

#### Erel

##### B4X founder
Staff member
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)?

#### thedesolatesoul

##### Expert
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.

#### Erel

##### B4X founder
Staff member
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

#### vpires

##### Member
Hello Erel,

By any change there is a #implements directive ?

#### Erel

##### B4X founder
Staff member
No. I also don't see how it can help.

(you can create a class that implements the interface and extends Activity and then use it.)

#### vpires

##### Member
@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
}

#### thedesolatesoul

##### Expert
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.

@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
}
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.basic4ppc.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.

#### thedesolatesoul

##### Expert
It works when i use the fully path to the class, I guess no need for imports.
Lets see if i can get this to work.
Thank you again Erel!

#### thedesolatesoul

##### Expert
Ok, I have got the tooleaps to be working fairly well (basic form). Just need to write some helper classes but actually almost no code is needed.
Erel this #extends thing is quite handy would help to port other library based activites too.
Thanks.

#### Erel

##### B4X founder
Staff member
Great. It (#Extends) was waiting for its moment to shine

#### warwound

##### Expert
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
#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.

#### Erel

##### B4X founder
Staff member
Why do you need to extend Activity?

It will be simpler to just expose onStartSession and onStopSession and let the developer call them from Activity_Resume and Activity_Pause.

Replies
45
Views
7K
Replies
0
Views
3K
Replies
18
Views
6K
Replies
123
Views
35K
Replies
421
Views
159K