|
|
|
|
|
|
|
|
|
show a new activity
|
|
|
Sequence
|
|
|
|
create the first activity
|
|
create an onClick-Event (button)
|
|
create the second activity (new activity)
|
|
create a new Intent-object
|
|
start the new activity
|
|
optional you receive a resultcode
|
|
|
|
|
|
|
|
|
|
1. Sample
|
|
|
1. Activity, onClick-Event
public void bn1_click(View view) {
Toast.makeText(this, "bn1_click", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
2. new Activity with params
First Activity, onClick-Event
public void bn1_click(View view) {
String param1 = "param1";
int param2 = 42;
Toast.makeText(this, "bn1_click", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,SecondActivity.class);
// set the params for the 2. activity
intent.putExtra("PARAM1", param1); // Hash value
// only String can set as a parameter !!!!!!!!!!
intent.putExtra("PARAM2", Integer.toString(param2)); // Hash value
startActivity(intent);
}
Second Activity, get the params
protected void onCreate(Bundle savedInstanceState) {
String message = intent.getStringExtra("param1");
edittext_param1.setText(message);
message = intent.getStringExtra("param2");
int param2=-1;
try {
param2 = Integer.parseInt(message);
} catch (NumberFormatException e) {
Toast.makeText(this, "NumberFormatException: param2", Toast.LENGTH_SHORT).show();
param2 = -1;
}
spinner_param2.setSelection(param2);
}
public void bnFinish_click(View view) {
finish();
}
|
|
|
|
|
|
|
|
|
2. Sample, with resultcode
|
|
|
new Activity with params and result code
First Activity, onClick-Event
public void bn1_click(View view) {
String param1 = "param1";
int param2 = 42;
Toast.makeText(this, "bn1_click", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,SecondActivity.class);
// set the params for the 2. activity
intent.putExtra("PARAM1", param1); // Hash value
// only String can set as a parameter !!!!!!!!!!
intent.putExtra("PARAM2", Integer.toString(param2)); // Hash value
startActivity(intent);
}
// This method get the resultcode or the requestCode
// in the "RESULT_OK" section you must insert yout code
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
// start the action
Toast.makeText(this, "start the action", Toast.LENGTH_LONG).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
} // if
} //onActivityResult
Second Activity, get the params
protected void onCreate(Bundle savedInstanceState) {
String str_param1 = intent.getStringExtra("param1");
edittext_param1.setText(str_param1);
int int_param2 = intent.getIntExtra("param2");
spinner_param2.setSelection(int_param2);
}
public void bnFinish_click(View view) {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
public void bnFinish_click(View view) {
Intent returnIntent = new Intent();
// set a param for the main activity
returnIntent.putExtra("result", "1111");
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
|
|
|
Spinner
TableLayout
|
|
|
|
|