0

here is the code from 1st activity:

package com.android.shopping;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

    Button btn;
    ListView lista;
    TextView tekst;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.button1);
        lista=(ListView) findViewById(R.id.listView1);
        btn.setOnClickListener(this);
        tekst=(TextView) findViewById(R.id.textView1);
    }

    @Override
    public void onClick(View v) {
        Intent i=new Intent(this, DetailsActivity.class);
        startActivityForResult(i, RESULT_OK);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK){
        String item=data.getStringExtra("data").toString();
        tekst.setText(item.toString());
    }

    }
    }

And the second one:

package com.android.shopping;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DetailsActivity extends Activity implements OnClickListener{
    Button save;
    EditText details;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        save=(Button) findViewById(R.id.button1);
        details=(EditText) findViewById(R.id.editText1);
        save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent in=new Intent();
        in.putExtra("data", details.getText().toString());
        setResult(RESULT_OK, in);
        finish();

    }


}

So in the first activity with the button I call the second activity. There user types in something in a textfield and click a button which brings him back to the first activity. And there the text he had typed should be shown in the textView from the first activity, but it is not. Please help me fix that.

Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72
user1477107
  • 37
  • 1
  • 8

2 Answers2

1

In your MainActivity class, you should change the line where you start DetailsActivity to

'startActivityForResult(i, 1);'

Note that you should not use RESULT_OK whose real value is -1. In sdk document, it says if you use a negative value as the 2nd parameter, startActivityForResult is simply treated as startActivity. That's why you can't get the result the DetailsActivity.

By the way, the 2nd paramter is the request_code. I don't think it's a good idea to make request_code and result_code the same.

Huang
  • 4,812
  • 3
  • 21
  • 20
  • @user1477107 Hello, perhaps you are a new comer to this site. If you find my answer solves your problem. You can give it a "tick" to accept my answer. This would encourage more people to help you in the futrue. – Huang Aug 12 '12 at 00:07
0

Are you sure your code goes past the setResult() and finish()?

onActivityResult() is invoked when you startActivityForResult(<new_activity_context>) and setResult() in the new activity.

Also, can you ensure your Manifest does NOT have android:launchMode="singleInstance"?

Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72