user8075840
user8075840

Reputation:

Passing textview value across activities

I am trying to pass a value to a text view into a different activity but only when the button in the activity is clicked.

Here is the activity I am trying to set the text...

public static Button yes;
public static final String TEST_KEY = "test";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question19);

    yes = (Button) findViewById(R.id.finalYes);
    yes.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
            startActivity(myIntent);

            Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
            i.putExtra(TEST_KEY, "SOME STRINGSSS");

        }
    });

Here is the HighRiskActivity that is the destination for updating the textview's value...

TextView t;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.high_risk);
        t = (TextView) findViewById(R.id.abusedOrNah);

        Bundle extras = getIntent().getExtras();
        if(extras != null)
        {
            String value = extras.getString(TEST_KEY);
            t.setText(value);
        }

My problem is that no text is printing at the desired activity, am I passing in the wrong data?? Any help would be amazing, this is the last step to the app that I am creating :D

UPDATE

I do not want the Question12Activity to be directed to the HighRiskActivity when the button is clicked. I want it to go to the next activity but still be able to pass the text onto the HighRiskActivity once the button is clicked. Sorry for the confusion, hopefully that makes more sense :)

Upvotes: 1

Views: 64

Answers (4)

Azeez Olaniran
Azeez Olaniran

Reputation: 665

I don't know what exactly you are trying to achieve by starting the first activity with the statements below

Intent myIntent = new Intent(v.getContext(), Question13Activity.class); startActivity(myIntent);

But if the Activity you want to start is the HighRiskActivity then the following should fix your issue:

  1. Get rid of the statements related to the Question13Activity mentioned above.
  2. and add the call to start the actual HighRiskActivity like below:

Intent i = new Intent(Question12Activity.this, HighRiskActivity.class); i.putExtra(TEST_KEY, "SOME STRINGSSS"); startActivity(i); //This line is important to start the new Activity.

So, I guess your major issue here is you didn't start the activity with the startActivity(i); call.

Upvotes: 1

Giannis
Giannis

Reputation: 121

You dont need to define two intents in order to connect two Activities. Put the following inside onClick method

Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i);

Upvotes: 0

H.P.
H.P.

Reputation: 1221

Try to pass data this way:

Intent i = new Intent(this, SecondActivity.class);

Bundle bundle = new Bundle();

//Add your data to bundle
bundle.putString(“test”, "Data you want to pass");

//Add the bundle to the intent
i.putExtras(bundle);

startActivity(i);

Now in your SecondActivity class you retrieve data as below:

Bundle bundle = getIntent().getExtras();

//get the data…
String stuff = bundle.getString(“test”); 

Upvotes: 1

ImJoe
ImJoe

Reputation: 21

In Activity A you are using the internal intent bundle that is not public to you and private to the intent. In the Activity B you are asking the intent to instead look for a bundle that you provided yourself.

Try something like this in your Activity A

    Intent intent = new Intent(context, YourActivity.class);
    Bundle bundle = new Bundle();
    bundle.putParcelable("EXTRA1", "string data");
    intent.putExtra(TEST_KEY, bundle);
    startIntent(intent);

Here is a good writeup on the subject (see answer) Advantages of using Bundle instead of direct Intent putExtra() in Android

Upvotes: 0

Related Questions