-3

I am struggling to figure out how to pass an int value from one activity to another. This is the activity where I initialize the variable:

    public class StartScreen extends AppCompatActivity {
// getting the start time
Calendar myDate = Calendar.getInstance();
int start = myDate.get(Calendar.DAY_OF_WEEK);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_screen);
}

// when clicking on start
public void startMap(View view){

...

and here is the other activity where I want to use that value:

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {
    ...
    public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

    // set kml layers
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    InputStream inputStream = null;

    try {
        // get different maps on different days of the week
        switch(start) {
            case Calendar.MONDAY:
    ...

How should I call the int start in the second activity? Does it happen by using Intent or SharedPreferences or maybe some other method?

ved
  • 11
  • 4
  • http://stackoverflow.com/questions/5424135/passing-integer-between-activities-and-intents-in-android-is-always-resulting-in – Quick learner Jan 12 '17 at 12:10

1 Answers1

1

Sender Activity:

Intent intent = new Intent(Sender.this, Reciever.class);
intent.putExtra("int_key", intValue);
startActivity(intent);

Receiver Activity:

 int value = getIntent().getIntExtra("int_key", 0); //0 important to use
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • When I try to use this method I get a couple of errors - 'cannot resolve putExtra' and when i put the variable in intValue it says it's an unknown class. How should I resolve these? – ved Jan 12 '17 at 13:22
  • let me check, where u putting these code? – W4R10CK Jan 12 '17 at 13:35
  • I'm using the sender activity code in my startscreen activity so it looks like: Calendar myDate = Calendar.getInstance(); int start = myDate.get(Calendar.DAY_OF_WEEK); Intent intent = new Intent(StartScreen.this, MapsActivity.class); intent.putExtra("start_key", start); startActivity(intent); and the receiver activity in my case is my mapsActivity – ved Jan 12 '17 at 14:07
  • Is it still showing the error, if yes what is the error. and how u fetching the value in MapsActivity.calss ? – W4R10CK Jan 12 '17 at 14:11
  • Here is where I'm fetching the value in the mapsActivity int value = getIntent().getIntExtra("start_key"); try { // get different maps on different days of the week switch(value) { ... but I get an error saying that 'getIntExtra(String, int) cannot be applied to String' – ved Jan 12 '17 at 14:17
  • Ohk, I got it use ` int value = getIntent().getIntExtra("start_key", 0);`. It will work now. Also see updated code. – W4R10CK Jan 12 '17 at 14:20
  • Thank you very much, that resolved all issues in the MapsActivity but it is still saying that it cannot resolve symbol 'putExtra' and throwing an issue with using 'unknown class' start when i've already defined it (intValue is start in my case) – ved Jan 12 '17 at 14:43
  • Assign `public int value;` globally and assign intent value in `onCreate` to this and it will be available to work globally. – W4R10CK Jan 12 '17 at 14:45