3

I am passing a value in intent between 2 activities to dynamically change data..

I am getting the value passed from the end activity in my home activity in the onCreate like so -

// Get Variable From Home Activity
    Bundle extras = getIntent().getExtras();
    String curhole;
    curhole = extras.getString("hole");
    TextView holeno = (TextView) findViewById(R.id.holeNumber);
    holeno.setText(String.valueOf("Hole " + curhole));

I have this onClick function in my home activity-

@Override
public void onClick(View v) {
    // Get Variable From Home Activity
    Bundle extras = getIntent().getExtras();
    String curhole;
    curhole = extras.getString("hole");

    Intent myIntent = new Intent(this, end.class);
    myIntent.putExtra("hole",curhole);
    startActivity(myIntent);

}

Then in my end activity I grab that value and pass it back to the home activity -

@Override
 public void onClick(View v) {
    Bundle extras = getIntent().getExtras();
    String curhole;
    curhole = extras.getString("hole");

    int incHole = Integer.parseInt(curhole);
    incHole++;
    String.valueOf(incHole);

    Intent myIntent = new Intent(this, hole.class);
    myIntent.putExtra("hole",incHole);
    startActivity(myIntent);

}

When I click the home activity button to go to the end activity and back to home it says the value is null.. Any idea why? I am new to android so im sure my approach is probably whack.

Thanks!

Ryan D
  • 741
  • 1
  • 11
  • 29

4 Answers4

2

Probably because you need to be getting the value in onResume() since it doesn't look like you are killing the Home Activity so onCreate() won't be called again.

You might want to make use of startActivityForResult() in this type of case though, passing this way will work.

Also, Intent has a [getIntExtra()](https://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String, int)) method so I'm not sure you need to do all of the converting you are doing.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
1

Share more. Why sending your int as String?

int incHole = Integer.parseInt(curhole);
incHole++;
//Should work as String or int
Intent myIntent = new Intent(this, hole.class);
myIntent.putExtra("hole", incHole);
startActivity(myIntent);

Let me know if helped. Check this link.

Community
  • 1
  • 1
Amg91
  • 165
  • 8
  • 25
  • good question, I have had trouble with passing integers for some reason.. Although yes I need to do it passing a int value to save myself the leg work.. – Ryan D Sep 02 '16 at 18:10
1

I would pass through intent from home activity to end activity the variable but starting the activity as startActivityForResult(); then on home activity you should rewrite onActivityResult(); method to get back the variable from end activity.

Check this tutorial of activity result read the 2.3. Retrieving result data from a sub-activity

AmirG
  • 615
  • 8
  • 18
0

When you press home then save bundle in saveInsatance() activity method.

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81