user2163853
user2163853

Reputation: 917

Passing data from one activity to another using bundle - not displaying in second activity

I'm currently trying to take data acquired through a REST API call, parse it for the information I need, and then pass that information to a new activity. I'm using the Asynchronous HTTP Client from loopj.com for the REST client, and then using the below code for my onClick and onCreate for the current and future activities, respectively.

Eclipse does not pass me any errors for any of my code, but when I try to run in the emulator, I get nothing (i.e. blank white screen) when the new activity/view opens. I've tried to code with a different URL in my REST CLIENT, but I still see nothing. I even took the API call out of the equation by commenting out the try/catch in onClick and changing venueName in the bundle.putString("VENUE_NAME", venueName); to searchTerm. Still, the new view comes up but nothing is displayed. What is not being passed, or what am I forgetting to make the second activity show the venueName?

public void onClick(View view) {
    Intent i = new Intent(this, ResultsView.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String searchTerm = editText.getText().toString();


    //call the getFactualResults method
    try {
        getFactualResults(searchTerm);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //Create the bundle
    Bundle bundle = new Bundle();
    //Add your data from getFactualResults method to bundle
    bundle.putString("VENUE_NAME", venueName);  
    //Add the bundle to the intent
    i.putExtras(bundle);

    //Fire the second activity
    startActivity(i);
}

Method in second activity that should receive the intent and bundle and display it:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Get the message from the intent
    //Intent intent = getIntent();
    //String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    //Get the bundle
    Bundle bundle = getIntent().getExtras();

    //Extract the data…
    String venName = bundle.getString(MainActivity.VENUE_NAME);        

    //Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(venName);

    //set the text view as the activity layout
    setContentView(textView);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

Thanks for your help. Very much appreciated.

Upvotes: 20

Views: 97819

Answers (8)

TDG
TDG

Reputation: 6161

There no need to use Bundle just to pass a string (or any primitive data type).
You can use i.putExtra("key", "value"); in the calling activity and
String s = getIntent().getStringExtra("key"); in the called activity.

Upvotes: 0

Asif Mehmood
Asif Mehmood

Reputation: 1

package com.example.mytest;

import androidx.annotation.MainThread;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import static com.example.mytest.MainActivity.STATIC_VARIABLE;

public class MainActivity2 extends AppCompatActivity {
   EditText editText1;
    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        editText1 = (EditText) findViewById(R.id.editText1);
        button1 = (Button) findViewById(R.id.button1);
        Bundle extras=null;
         extras=this.getIntent().getExtras();
        if(extras!=null) {
          String recieved =extras.getString("TAG");
          editText1.setText(String.valueOf(recieved));
        }
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
  Intent backIntent = new Intent(MainActivity2.this, MainActivity.class);
                String input= editText1.getText().toString();
                backIntent.putExtra("TAG2",input);
               STATIC_VARIABLE=3;
//    setResult(RESULT_OK,intent);
//                startActivityForResult(intent, 2);
                startActivity(backIntent);


            }
        });
    }


    }

Upvotes: 0

Anand Phadke
Anand Phadke

Reputation: 531

Intent loginIntent = new Intent(LoginActivity.this, HomeActivity.class);

Bundle bundle = new Bundle(); 


bundle.putString("user_id", userId);

i.putExtras(bundle);

startActivity(loginIntent);

LoginActivity.this.finish();

Upvotes: 0

krtkush
krtkush

Reputation: 1528

I think if you replace

String venName = bundle.getString(MainActivity.VENUE_NAME); 

with

String venName = bundle.getString("VENUE_NAME");

it should work.

Here is how I handle my transfer of data from one activity to another:

Sending data to activity called "Projectviewoptions":

Bundle b = new Bundle();
          b.putString("id", str_projectid);
          Projectviewoptions pv = new Projectviewoptions();

Receiving data:

idbundle = getArguments();
String myid = idbundle.getString("id");

The "key" on both sides should be same; in this case "id".

Another way to send data, through intent is:

Send:

Intent intent = new Intent(getActivity(),ViewProjectDetails.class);
                            intent.putExtra("id", myid);
                            startActivity(intent);

Recieve:

String id = getIntent().getExtras().getString("id");

Upvotes: 5

GrIsHu
GrIsHu

Reputation: 23638

You are wrongly accessing the key which you have added in bundle. Besides getting the String as MainActivity.VENUE_NAME try to directly pass the key name which you have added in bundle as below:

Besides getting string as below :

   //Get the bundle
     Bundle bundle = getIntent().getExtras();
    //Extract the data…
   String venName = bundle.getString(MainActivity.VENUE_NAME);        

Try to get the string using key name as below:

    /Get the bundle
     Bundle bundle = getIntent().getExtras();
    //Extract the data…
   String venName = bundle.getString("VENUE_NAME");  

Upvotes: 4

Siddharth Lele
Siddharth Lele

Reputation: 27748

Two ways you can send the data. This is how you are sending it at the moment. And there is nothing wrong with it.

//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);

In you code (second Activity) however, you are referring to the key in the Bundle as MainActivity.VENUE_NAME but nothing in the code suggests that you have a class that returns the value as the actual key name send with the Bundle. Change your code in the second Activity to this:

Bundle bundle = getIntent().getExtras();

//Extract the data…
String venName = bundle.getString("VENUE_NAME");        

//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(venName);

You can check in your second Activity if the Bundle contains the key using this and you will know that the key is not present in the Bundle. The correction above, however, will get it working for you.

if (bundle.containsKey(MainActivity.VENUE_NAME))    {
    ....
}

Upvotes: 32

Andre Viau
Andre Viau

Reputation: 275

to send bundle.

Bundle bundle = new Bundle();
bundle.putString("Name",Object); //This is for a String
i.setClass(CurrentClass.this, Class.class);
i.putExtras(bundle);
startActivity(i);

to receive bundle

Bundle bundle = null;
bundle = this.getIntent().getExtras();
String myString = bundle.getString("Name");//this is for String  

Upvotes: 1

Karakuri
Karakuri

Reputation: 38605

Make sure the String you used as the key to put the item into your Bundle is the same as the key used to extract it. In your case, perhaps MainActivity.VENUE_NAME is not the same as "VENUE_NAME"

Upvotes: 0

Related Questions