1

I want to pass some audio name to the previous Activity in my Android App.I tried a lot of ways.I don't know where I'm making mistake.I just want to send record audio name to previous class.

Here is my code record_Audio.

btnSaveRecord = (Button)findViewById(R.id.buttonSaveRecord);
            btnSaveRecord.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                   /* Intent intent = new Intent(Record_Audio.this , AddPost.class);
                    intent.putExtra("STRING_I_NEED", newAudioFile);
                    setResult(REQUEST_CODE, intent);
                    finish();*/

                    Intent intent = new Intent(Record_Audio.this, AddPost.class);
                    // Add any data that you wish to send
                    intent.putExtra("DATA", newAudioFile);
                    startActivityForResult(intent, REQUEST_CODE);

                    //Intent intent = new Intent(Record_Audio.this , AddPost.class);
                    //startActivity(intent);
                    //finish();

                }
            });

In previous code where i want to receive AudioName = AddPost activity

 protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        try
        {
            if (requestCode == REQUEST_CODE)
            {
                if (resultCode == RESULT_OK)
                {
                    String newString = data.getExtras().getString("STRING_I_NEED");
                    Log.e("newString "," = "+newString);
                    String valueToChange = data.getExtras().getString("DATA");
                    Log.e("valueToChange "," = " + valueToChange);

                }
            }

            if (requestCode == CAMERA_REQUEST)
            {  //Capture Camera Image }

            if (requestCode == RESULT_LOAD_IMG)
            {  // //Capture Gallery Image}
Prachi
  • 2,559
  • 4
  • 25
  • 37
androidTag
  • 5,053
  • 7
  • 19
  • 28

3 Answers3

2

Please refer to this post : How data can be passed between two activities

It Will provide you the way as well as example for this purpose.

Community
  • 1
  • 1
Er.Rohit Sharma
  • 696
  • 5
  • 21
2

First of all you can not use startActivity when you are calling RecordAudio activity from AddPost activity. You need to use startActivityForResult()

calling RecordAudio from AddPost use below code :

Intent intent=new Intent(AddPost.this, RecordAudio.class);
startActivityForResult(intent, 1010);

In your RecordAudio file

getIntent().putExtra("data", "your data");
setResult(RESULT_OK, getIntent());
finish();

and in AddPost onActivityForResult()

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==1010 && resultCode==RESULT_OK)
    {
           String newString = data.getExtras().getString("data");
           Log.e("newString "," = "+newString);
    }
}
Ravi
  • 34,851
  • 21
  • 122
  • 183
0

Here i have provided simple demo that explains how to start activity for result :

Here is my FirstActivity :

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class FirstActivity extends Activity {

    private Button btnFirstActivity;
    private static final String TAG = FirstActivity.class.getSimpleName();
    public static final String KEY_FILENAME = "filename";
    public static final int REQUEST_CODE = 0;
    public static final int RESPONSE_CODE = 1;

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

        btnFirstActivity = (Button) findViewById(R.id.btn_first);
        btnFirstActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                intent.putExtra(KEY_FILENAME, "audiofile.mp3");
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESPONSE_CODE) {
            Log.e(TAG, "-------GOT RESPONSE CODE--------");
            Log.e(TAG, "DATA IS : " + data.getStringExtra(KEY_FILENAME));
        }
    }
}

Layout for FirstActivity : activity_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_first"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Second Activity" />

</LinearLayout>

Here is my SecondActivity :

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends Activity {

    private Button btnSecondActivity;
    private static final String TAG = SecondActivity.class.getSimpleName();
    private String strFileName = null;
    private SecondActivity secondActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        secondActivity = SecondActivity.this;
        strFileName = getIntent().getStringExtra(FirstActivity.KEY_FILENAME);
        btnSecondActivity = (Button) findViewById(R.id.btn_second);
        btnSecondActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < 50; i++) {
                    Log.e(SecondActivity.TAG, "------PRINTING-----" + i);
                }

                Intent intent = new Intent();
                intent.putExtra(FirstActivity.KEY_FILENAME, strFileName);
                setResult(FirstActivity.RESPONSE_CODE, intent);
                secondActivity.finish();
            }
        });
    }

}

Layout for SecondActivity : activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <Button
        android:id="@+id/btn_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start First Activity" />

</LinearLayout>

Hope It helps :)

Thanks..!!

AndiGeeky
  • 11,266
  • 6
  • 50
  • 66