0

So I have been using the getIntent method to send my original int value, set to 10, to a second class.

Intent webClick = new Intent(this, WebsearchIAP.class);
webClick.putExtra("cred", cred);
startActivity(webClick);

Then, I update the variable by subtracting 1 from it.

Intent get = getIntent();
int cred = get.getIntExtra("cred", 10);

public void buyClick(View view) {
    sCredits.setText(cred - 1);
}

Then, I want to send it back to my MainActivity, by doing this.

public void backClick(View view) {
    Intent close = new Intent(this, MainActivity.class);
    close.putExtra("cred", cred);
    startActivity(close);
}

Once back in my MainActivity, this is what I would like to do.

@Override
public void onResume() {
    super.onResume();
    Intent close = getIntent();
    int cred = get.getIntExtra("cred", [VALUE OF CRED IN PREVIOUS ACTIVITY]);
}

My problem is the getIntent method requires the numerical value of "cred", which would be no big deal if I was using the method only once, but I want to use it every time this app resumes the main screen, hence the call to onResume. So, how do I set my app to take the value of "cred" in the previous activity, no matter what it may be, in one method?

Sam Mader
  • 1
  • 1
  • I'm hesitant to mark as a dupe because I think you might be asking multiple questions here but, you should be using [startActivityForResult](http://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541) – codeMagic Jan 01 '16 at 02:54
  • I didn't get you. Do you want cred to be accessed in all activities? Or you want to just pass the variable to other activity and receive it back after processing? – S Praveen Kumar Jan 01 '16 at 04:12
  • You can also create an interface in second Activity and implement it in first Activity. – S Praveen Kumar Jan 01 '16 at 04:17

2 Answers2

0

Maybe you can use SharedPreferences. It is commonly used for storing app preferences and is shared in the application.

Firstly, get your SharedPreferences in your activity:

private SharedPreferences sp = getSharedPreferences("foo", MODE_PRIVATE);

and then set and put value like this:

int cred = sp.getInt("CRED", 10);        // Put this statement in onResume
sp.edit().putInt("CRED", cred).apply();

You can get offical docs for SharedPferences here.

Perqin
  • 755
  • 10
  • 27
0

There is a best way for doing this. Send an intent to second activity from first using startActivityForResult()instead of startActivity(). Then from second Activity use setResult() to send the value back to the first Activity. Override onActivityResult() in first activity to get the updated value. Example:

 //First Activity
 Intent intent=new Intent(getApplicationContext(),secondActivity.class);
 intent.putExtra("cred",cred);
 startActivityForResult(intent,RESULT_CODE);

 //second Activity
 Intent i=getIntent();
 cred=i.getIntExtra("cred",10);
 //....Update your values
 Intent sendBack=new Intent();
 sendBack.putExtra("cred",cred);
 setResult(RESULT_CODE,sendBack);

 //First Activity
 @Override
 protected void onActivityResult(int reqCode, int resCode, Intent data)
 {
 if(resCode==RESULT_CODE)
 cred=data.getIntExtra("cred",10);
 }
 //RESULT_CODE is a integer to be kept constant for a type of result.
S Praveen Kumar
  • 341
  • 1
  • 13