将int值从一个活动传递到另一个活动

时间:2017-01-12 12:08:53

标签: java android

我正在努力弄清楚如何将int值从一个活动传递到另一个活动。 这是我初始化变量的活动:

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

...

这是我想要使用该值的另一​​个活动:

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

如何在第二个活动中调用int start?是通过使用Intent或SharedPreferences还是其他方法来实现的?

1 个答案:

答案 0 :(得分:1)

发件人活动:

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

接收者活动:

 int value = getIntent().getIntExtra("int_key", 0); //0 important to use
相关问题