Reputation: 480
i want to open an Activity using Intents By following this code:
Intent i=new Intent("com.example.learn.CONNECTION");
@Override
protected void onCreate(Bundle savedInstanceState) {
.
.
.
server=(Button) findViewById(R.id.Server);
client=(Button) findViewById(R.id.Client);
server.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
i.putExtra("check", "server");
startActivity(i);
// TODO Auto-generated method stub
}
});
client.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
i.putExtra("check1","client");
startActivity(i);
}
}); Now In this Connection Activity I am checking it with:
if(getIntent().hasExtra("check"))
value = getIntent().getExtras().getString("check");
if(getIntent().hasExtra("check1"))
value = getIntent().getExtras().getString("check1");
But the code is not working it either receives client or server. How should i solve this problem is there any other way out. Any other suggestions not related to this code will also be accepted.
Upvotes: 0
Views: 1977
Reputation: 430
dont use different vars like "check" or "check 1" use a flag . this code will save your problem
server.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
i.putExtra("check", "server");
startActivity(i);
// TODO Auto-generated method stub
}
});
client.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
i.putExtra("check","client");
startActivity(i);
}
in your next activity, just use this code .
value = getIntent().getStringExtra("check");
if(value.equals("server")){
//server code
}
else if(value.equals("client")){
//client code
}
You can also use some boolean flags in the same way if your server and client values are dynamic.
Upvotes: 0
Reputation: 430
Dear you are doing a mistake. the sample you provided is used to launch web urls normally. if you want to start another intent then you will have to change the first line only.
Intent intent = new Intent(CurrentActivity.this , NextActivity.class);
Try this please.
Upvotes: 1