Here I used two Activity classes to pass data from one activity to another activity through Intents.
Main Activity.java
: which Intent send the data:
public class MainActivity extends Activity implements
OnClickListener {
EditText name,
pwd;
Button
sub;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name=(EditText)findViewById(R.id.NameText);
pwd=(EditText)findViewById(R.id.PassWordText);
sub=(Button)findViewById(R.id.button1);
sub.setOnClickListener(this);
}
@Override
public
void onClick(View v) {
String
ename=name.getText().toString();
String
epwd=pwd.getText().toString();
Intent
intn=new Intent(MainActivity.this,SecondActivity.class);
// here putExtra(keyvalue,value); // here keyvalue is a key
value to identify value from first intent in another intent. Value is value
which you are passing to another intent.
intn.putExtra("Name",ename);
intn.putExtra(
"PassW",epwd);
startActivity(intn);
} }
Second Activity.java:
which receives that data and prints that data:
public class SecondActivity extends Activity implements
OnClickListener{
Button
get,search;
EditText
nm,pw;
String
sname,spwd;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
get=(Button)findViewById(R.id.button);
nm=(EditText)findViewById(R.id.editName);
pw=(EditText)findViewById(R.id.editPassWord);
search=(Button)findViewById(R.id.Searchbutton);
search.setOnClickListener(this);
get.setOnClickListener(this);
//Bundle is using to get the intent extra fileds from
passing intent.
Bundle
b=getIntent().getExtras();
sname=b.getString("Name");
spwd=b.getString("PassW");
}
@Override
public
void onClick(View v) {
//
TODO Auto-generated method stub
if(v==get){
nm.setText(sname);
pw.setText(spwd);
}
else
if(v==search){
Intent
intn=new Intent(Intent.ACTION_VIEW);
intn.setData(Uri.parse("http://www.google.com"));
startActivity(intn);
} } }
No comments:
Post a Comment