hakuna matata
hakuna matata

Reputation: 3327

Sending data from one activity to another before finish

I wanted to transfer some data from an activity to the previous activity. I simplified it by first testing the sending of data, so I made a project which has 2 activities, MainActivity and SecondActivity, SecondActivity has a text field and a button, and MainActivity has a text view. I looked on SO for my problem, I found a lot that said to use startActivityForResult and I found some samples that helped me to write code. However, it doesn't seem to work. I tried to print out the string values. The code and output are posted below.

MainActivity:

package com.example.activitytesting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    private static final int REQUEST_CODE_TEST = 0;

    private TextView txtView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtView = (TextView) findViewById(R.id.txtView);

        Intent i = new Intent(MainActivity.this, SecondActivity.class);
        startActivityForResult(i, REQUEST_CODE_TEST);
    }

    @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;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == REQUEST_CODE_TEST) {
            if(resultCode == Activity.RESULT_OK) {
                String txt = data.getStringExtra("txt");
                System.out.println("from main activity: \"" + txt + "\"");
                txtView.setText(txt);
            }
        }
    }
}

SecondActivity:

package com.example.activitytesting;

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

public class SecondActivity extends Activity {
    private String txtStr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        EditText txt = (EditText) findViewById(R.id.txtField);
        txtStr = txt.getText().toString().isEmpty() ? "empty text field" : txt.getText().toString();

        Button finishBtn = (Button) findViewById(R.id.finishBtn);
        finishBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent result = new Intent();
                System.out.println("from second activity: \"" + txtStr + "\"");
                result.putExtra("text", txtStr);
                setResult(Activity.RESULT_OK, result);
                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.second, menu);
        return true;
    }

}

Now I got the output from LogCat, which are:

12-05 01:28:34.850: I/System.out(359): from second activity: "empty text field"
12-05 01:28:34.900: I/System.out(359): from main activity: "null"

Why am I not able to get the data from one activity to another?

Upvotes: 0

Views: 777

Answers (1)

Andrew Fielden
Andrew Fielden

Reputation: 3899

Your resource bundle keys don't match

Second activity is using

result.putExtra("text", txtStr);

But MainActivity retrieves using

String txt = data.getStringExtra("txt");

Both activities should use the same key - use either 'text' or 'txt' in both cases

Upvotes: 3

Related Questions