Java Question Can't start activity from a custom library

NeverGiveUp2

Member
Licensed User
Longtime User
Hi,
i want to start a activity that is build in a custom library. I got no error messages, but it doesen't work. I see the log informtin in b4a.

Here is some code:
B4X:
#Region  Project Attributes 
   #ApplicationLabel: B4A Example
   #VersionCode: 1
   #VersionName: 
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: False
#End Region

#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.
   Dim cmp20 As CitizenCMP20
   Dim Button1 As Button
   Dim Button2 As Button
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("TestPrint")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub



Sub Button1_Click
   Log("Button1_Click")
   'i call the function start() from CitizenCMP20 here
   cmp20.Start
   
End Sub
Sub Button2_Click
   
End Sub

Here is the 1. class of the library:

B4X:
package com.mcdreck.citizentest;

import android.content.Intent;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;

@Permissions(values = {"android.permission.BLUETOOTH", "android.permission.BLUETOOTH_ADMIN", "android.permission.WRITE_EXTERNAL_STORAGE"}) 
@DependsOn(values={ "Citizen_Android_1056" })
@Author("NeverGiveUp2") 
@Version((float) 1.07)

@ShortName("CitizenCMP20")

public class CitizenCMP20 {
   public CitizenCMP20(){
      
   }
      
   public void Start(){
      Common.Log("for new Intent");
      Intent in = new Intent( BA.applicationContext, TestActivity.class);
      Common.Log("forr startActivity");
        startActivity(in);  
   }

   private void startActivity(Intent in) {
      // TODO Auto-generated method stub
      
   }
   

}

Here is the activity:
B4X:
package com.mcdreck.citizentest;

import android.content.Intent;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;

@Permissions(values = {"android.permission.BLUETOOTH", "android.permission.BLUETOOTH_ADMIN", "android.permission.WRITE_EXTERNAL_STORAGE"}) 
@DependsOn(values={ "Citizen_Android_1056" })
@Author("NeverGiveUp2") 
@Version((float) 1.07)

@ShortName("CitizenCMP20")

public class CitizenCMP20 {
   public CitizenCMP20(){
      
   }
      
   public void Start(){
      Common.Log("for new Intent");
      Intent in = new Intent( BA.applicationContext, TestActivity.class);
      Common.Log("forr startActivity");
        startActivity(in);  
   }

   private void startActivity(Intent in) {
      // TODO Auto-generated method stub
      
   }
   
       }
}

I need heeeeeelp. :BangHead:
Thank you for reading :)
 

warwound

Expert
Licensed User
Longtime User
First:

B4X:
    public void Start(){
        Common.Log("for new Intent");
        Intent in = new Intent( BA.applicationContext, TestActivity.class);
        Common.Log("forr startActivity");
          startActivity(in);  
    }

That should be:

B4X:
    public void Start(BA pBA){
        Common.Log("for new Intent");
        Intent in = new Intent( pBA.activity, TestActivity.class);
        Common.Log("forr startActivity");
        startActivity(in);  
    }

Second you need to add an entry to the manifest declaring the TestActivity:

B4X:
AddApplicationText(<activity android:name="your.library.package.name.TestActivity"/>)

Martin.
 
Last edited:

NeverGiveUp2

Member
Licensed User
Longtime User
Hi Martin,

thank you for answer.

Example: Do i call

Dim cmp20 As CitizenCMP20

And in the click-Event

cmp20.start(Activity) ' Is this right?

And in which position i put the entry in the b4a-manifest. Can you give me a short example.

Best regards
Roland
 

warwound

Expert
Licensed User
Longtime User
See this post of mine for some more info: http://www.b4x.com/forum/libraries-developers-questions/18730-start-library-activity.html#post107539

You add the manifest text anywhere but to keep things tidy simply add it to the end of the default manifest text:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

AddApplicationText(<activity android:name="your.library.package.name.TestActivity"/>)

Your b4a code to call the Start method is simply:

B4X:
cmp20.Start

b4a wil automatically pass the BA object to all methods in your library - it does not require you to manually pass any parameter in the b4a method call.

If a library method signature contains a BA parameter then b4a automatically passes the BA object to your library method.

Martin.
 
Top