Вопрос

hi , I am writing a code in eclipse that works as follows:

  • click on the button to take me to second activity
  • in second activity , iam adding 2 numbers and send the sum of them to first activity.
  • in the first activity it should popup a message containing the sum of the two numbers . Every thing is okk , except displaying the message , its not appearing.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClick(View view){
        startActivityForResult(new Intent("co.example.SECONDACTIVITY"),request_code);
    }
    public void onActivityResult(int result,int requestcode,Intent data)
    {
        if(requestcode == request_code)
        {
            if(result == RESULT_OK)
            {
                Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();
            }
        }
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    }
    

second activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_secondactivity);
}
public void onClick(View view){
    EditText b1 = (EditText) findViewById(R.id.editText1);
    EditText b2 = (EditText) findViewById(R.id.editText2);
    int x =Integer.parseInt(b1.getText().toString())+ Integer.parseInt(b2.getText().toString());
    String s = Integer.toString(x);
    Intent data = new Intent();
    data.setData(Uri.parse(s));
    setResult(RESULT_OK,data);
    finish();

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.secondactivity, menu);
    return true;
}

}

Это было полезно?

Решение

Try this..

I think you messed up with parameters. You have wrongly comparing parameters.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestcode == request_code)
    {
        if(resultCode == RESULT_OK)
        {
            Toast.makeText(this,data.getData().toString(),Toast.LENGTH_SHORT).show();
        }
    }

}

Другие советы

First Make sure your result code is same and If it is coming with same Request Code na you just Print like this and try to print your Data..In your Toast...And increase your Toast timing

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data)
  {
     if(requestcode == request_code)
     {
       if(resultCode == RESULT_OK)
       {
          Toast.makeText(ActivityName.this,data.getData().toString(),Toast.LENGTH_LONG).show();
       }
     }
  }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top