-1

I'm using startActivityForResult, but I have no data, why? with this code I lunch the activity

conto = (Button) findViewById(R.id.btn_new_conto);
conto.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Intent intent=new Intent(Mov.this,onto.class);
        startActivityForResult(intent, 2);

and with this code I pass the value:

  lista.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                Dettaglio d = dettagli.get(position);
             Intent intent=new Intent();
                 intent.putExtra("conto", d.conto);
                 setResult(2,intent);
finish();

with this code I get the value:

protected void onActivityResult1(int requestCode, int resultCode, Intent data){
          super.onActivityResult(requestCode, resultCode, data);
                          if(requestCode==2){
               if(null!=data) { 
                  conto.setText(data.getStringExtra("conto"));
  • "...but I have no data,... " Could you explain that a little more? – codeMagic Dec 17 '13 at 21:21
  • 1
    you didn't post the code where you receive the value ... – njzk2 Dec 17 '13 at 21:22
  • 1
    To clarify: Show your onActivityResult() method. If you don't have one, write one. – Dale Wilson Dec 17 '13 at 21:23
  • At first the setResult method should be called like this: `setResult(Activity.RESULT_OK, intent);`. Also post your `onActivityResult` code. – vortexwolf Dec 17 '13 at 21:25
  • 1
    @vorrtex no you are wrong. The first paramter is an int value – Blackbelt Dec 17 '13 at 21:27
  • 2
    Is your onActivityResult1 a misspelled name? It should not has the number 1 on its name – noni Dec 17 '13 at 21:27
  • why do you have onActivityResult1 ? – Blackbelt Dec 17 '13 at 21:28
  • @blackbelt I always right, here as well, the first parameter should be `Activity.RESULT_OK` and not `2`. – vortexwolf Dec 17 '13 at 21:29
  • what does "I always right" mean. Still you are wrong. You can put the int value you want. It will be received as resultCode in onActivityResult. Please read the documentation @vorrtex – Blackbelt Dec 17 '13 at 21:30
  • @blackbelt `You can` doesn't mean `you should`. The first parameter of `setResult` is `Activity.RESULT_OK` or 2 other constants, not "any int value". – vortexwolf Dec 17 '13 at 21:32
  • you wrote "At first the setResult method should be called like this". It is like you are telling him that it does not work because the first parameter is 2 instead of Activity.RESULT_OK. @vorrtex – Blackbelt Dec 17 '13 at 21:34
  • ok guys, so how can I? – user2856560 Dec 17 '13 at 21:35
  • ok, this question was offropic anyway. @user2856560 replace your `onActivityResult1` and see answers there: http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result – vortexwolf Dec 17 '13 at 21:38

2 Answers2

0

ActivityForResult is an asynchronic call. You should override onActivityResult() from your initial Activity, there you will get the result of the second Activity.

Inside your second Activity you should call setResult() method, continued by finish()

noni
  • 2,927
  • 19
  • 18
-1

In the started activity you just have to call setResult(int) and then finish(). In the calling activity you catch the result in onActivityResult(int, int, Intent)

Read more about this in the developers guide

mach
  • 8,315
  • 3
  • 33
  • 51