0

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.

Shashank Sood
  • 480
  • 5
  • 16

2 Answers2

1

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.

Syed_Adeel
  • 430
  • 5
  • 14
  • u Didn't get my point. I am able to start another intent but the problem is that the string i am passing with the key value "check" or "check1" is received by the Connection Activity class with the value client or server. Not both. To be more precise i am opening same application on two phones On one side i click server and on the other i click client but it only receives one value "server" or "client" from the putExtra() method – Shashank Sood May 22 '13 at 12:38
0

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.

Syed_Adeel
  • 430
  • 5
  • 14