Skip to content

Instantly share code, notes, and snippets.

@emoa2l
Created June 5, 2011 03:33
Show Gist options
  • Save emoa2l/1008617 to your computer and use it in GitHub Desktop.
Save emoa2l/1008617 to your computer and use it in GitHub Desktop.
Android: Create a new window(activity) and pass parameters to it.
public class Activity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
// in the calling activity define a bundle that can contain
// some number of strings
Bundle bundle = new Bundle();
bundle.putString("paramter", "value");
// create the intent to launch the new activity
Intent i = new Intent(Activity1.this, Activity2.class);
// attach the bundle to the intent and then start it
i.putExtras(bundle);
startActivityForResult(i,0);
}
}
public class Activity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showdefinition);
// initialize a bundle and set it to the current intents bundle
Bundle bundle = this.getIntent().getExtras();
TextView tv = (TextView) findViewById(R.id.<viewId>);
// you can now reference any strings defined in the bundle in
// the calling activity
tv.setText(bundle.getString("paramter"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment