1

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 :)

  • Welcome to SO! (1) Is this homework? If so, it should be marked as such. (2) What activity does the 2nd code sample belong to: Question13Activity or HighRiskActivity? It seems to be HighRiskActivity, but you're not launching HighRiskActivity. – LarsH Jul 20 '17 at 19:54
  • yes exactly we are confused with your code on which activity you want to set your text? – Deep Naik Jul 20 '17 at 19:56
  • This is not homework it is an app that I am making for someone... The second code sample signifies the HighRiskActivity, which is where I want the TextView's values to be set –  Jul 20 '17 at 19:57
  • Sorry for the confusion, i edited my question –  Jul 20 '17 at 19:57
  • 1
    you dont have a startActivity for that.. – Deep Naik Jul 20 '17 at 19:59
  • Won't the startActivity direct that button to that HighRiskActivity?... I don't want that to happen –  Jul 20 '17 at 19:59
  • 2
    Only one activity is active at once. There is no benefit calling startActivity twice in a row – OneCricketeer Jul 20 '17 at 20:00
  • Also, the fact that you have numbered Activities seems wrong... What is changing between Question12 and 13? Just text? questions and answers? You realistically only need one activity for both. – OneCricketeer Jul 20 '17 at 20:02
  • It's for different questions –  Jul 20 '17 at 20:02
  • A `Question` can be one Java object. A `QuestionActivity` should be able to display *any Question* object – OneCricketeer Jul 20 '17 at 20:03
  • The questions have different displays, it's not all just different questions with text on them. Each one has it's own layout and such thats why I did it –  Jul 20 '17 at 20:04

4 Answers4

1

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”); 
H.P.
  • 1,181
  • 1
  • 13
  • 26
1

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.

Azeez Olaniran
  • 625
  • 1
  • 6
  • 11
  • I want to be able to continue going through my question activites but still be able to generate the text and store it in the HighRiskActivity. –  Jul 20 '17 at 20:27
  • I have yet to do that, the solution that someone gave above started the HighRiskActivity, which is not what I want even though it gave me the text, it didn't continue on with the questions –  Jul 20 '17 at 20:28
  • @AaronAllen: Then you probably need to edit your question to explain that you don't want to start the HighRiskActivity, because that's confusing everyone. You created an intent for HighRiskActivity, which is only used for starting HighRiskActivity. Explain that you want to run some code in HighRiskActivity without starting it. – LarsH Jul 20 '17 at 20:38
  • @LarsH Thanks I made an update to my question clarifying the situation –  Jul 20 '17 at 20:44
0

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

ImJoe
  • 21
  • 4
0

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);
Giannis
  • 121
  • 2
  • 4
  • 13