Java Question Java Library and StartActivityForResult

stefanobusetto

Active Member
Licensed User
Longtime User
Here an example:

1) Create a java lib with the following 2 classes:

--- first class ---
@ShortName("xnButtonActivity")
@ActivityObject

public class xnButtonActivity extends ViewWrapper<Button> {
private Button obj ;

@Override
@Hide
public void innerInitialize ( final BA ba , final String eventName , boolean keepOldObject )
{ if ( !keepOldObject )
{ obj = new Button ( ba.context ) ;
obj.setBackgroundColor ( Color.RED ) ;

obj.setOnClickListener ( OnClickListener ) ;
setObject ( obj ) ;
} ;

super.innerInitialize ( ba, eventName, true ) ;
} ;

private View.OnClickListener OnClickListener = new View.OnClickListener ()
{ @Override
public void onClick ( View v )
{ obj.setBackgroundColor(Color.YELLOW) ;
obj.setText("start");

Intent i = new Intent ( ba.context , xnSearch.class ) ;

final IOnActivityResult r = new IOnActivityResult()
{ @Override
public void ResultArrived ( int result , Intent intent )
{ if ( result == 1 )
{ obj.setText ( "ok" ) ;
}
if ( result == 2 )
{ obj.setText ( "cancel" ) ;
}
} ;
} ;

ba.startActivityForResult (r , i ) ;
} ;
} ;
}

--- second class ---
@ShortName("xnSearch")

public class xnSearch extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll ;
Button b1 ;
Button b2 ;


ll = new LinearLayout ( this ) ;
ll.setOrientation(LinearLayout.VERTICAL);

b1 = new Button(this) ;
b1.setText("ok");
ll.addView(b1,200,90);

b2 = new Button(this) ;
b2.setText("cancel");
ll.addView(b2,200,90);

setContentView(ll);

b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(1, intent);
finish();
}

});

b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(2, intent);
finish();
}

});

}
}

2) Create a b4a project.
3) Add a reference to the lib.
4) Add to the manifest of the b4a the following:
AddApplicationText(
<activity android:name="it.csinet.xnObjects.xnSearch" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
)
5) Obviously replace "it.csinet.xnObjects"
6) Add an xnButtonActivity to your b4a application.
7) Start the application.

When you click the button the xnSearch activity is started.
When you click on the "ok" or "cancel" buttons of the xnSearch activity the
xnSearch finishes and you can see the exit code as the caption of the button.
:D
 
Last edited:
Top