我正在尝试将变量从一个活动发送到另一个活动,而我已经设置了要发送到第二个活动的意图。我想知道的是在第二个活动中,我需要做什么才能能够在if语句中使用该变量?

这是我的代码

Intent mainIntent = new Intent(TheLeagueActivity.this,IntroActivity.class);

                                    mainIntent.putExtra("leagueCount", leagueCount);

                                    TheLeagueActivity.this.startActivity(mainIntent);

                                    TheLeagueActivity.this.finish();

最佳答案

onCreate()IntroActivity方法中编写以下代码

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Intent intent = getIntent();

    // -1 is default value if no value associated with key "leagueCount"

    int leagueCount = intent.getIntExtra("leagueCount", -1);

   /*
      if leagueCount is not equal to -1
      use leagueCount here

   */
}

10-07 22:18