活动堆栈无法正常工作

时间:2017-10-18 08:38:36

标签: java android android-intent android-activity activity-stack

我有一个具有MainActivity的应用。

如果首次启动,它会启动一个显示简介滑块的活动,如果没有,则会启动一个MainWeatherActivity。

以下是MainActivity的代码

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    boolean firstStart = PreferenceManager.getDefaultSharedPreferences(this)
            .getBoolean(PREF_KEY_FIRST_START, true);

    Log.i("MainActivity", "firstStart = " + Boolean.toString(firstStart));

    if (firstStart) {
        Intent i = new Intent(this, MainIntroActivity.class);
        startActivityForResult(i, REQUEST_CODE_INTRO);
    }
    startActivity(new Intent(this, MainWeatherActivity.class));
    finish();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_INTRO) {
        if (resultCode == RESULT_OK) {
            PreferenceManager.getDefaultSharedPreferences(this).edit()
                    .putBoolean(PREF_KEY_FIRST_START, false)
                    .apply();
        } else {
            PreferenceManager.getDefaultSharedPreferences(this).edit()
                    .putBoolean(PREF_KEY_FIRST_START, true)
                    .apply();
            //User cancelled the intro so we'll finish this activity too.
            finish();
        }
    }
}

当我第一次打开应用程序时,用户应该看到MainIntroActivity,然后是MainWeatherActivity。 但是这段代码直接启动了MainWeatherActivity,当我按下后退按钮时,它会启动MainIntroActivity。

我哪里出错了,如何解决这个问题?

MainIntroActivity

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("MainIntroActivity","onCreate");
    addSlide(new SlideFragmentBuilder()
                    .backgroundColor(R.color.colorPrimary)
                    .buttonsColor(R.color.colorAccent)
                    .neededPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION})
                    .image(agency.tango.materialintroscreen.R.drawable.ic_next)
                    .title("title 3")
                    .description("Description 3")
                    .build(),
            new MessageButtonBehaviour(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showMessage("We provide solutions to make you love your work");
                }
            }, "Work with love"));
}

MainWeatherActivity

        LocationManager mLocationManager;
double latitude, longitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_weather);
    Log.i("MainActivity","onCreate");
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(MainWeatherActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Toast.makeText(MainWeatherActivity.this,"Successful. Latitude ="+Double.toString(latitude)+" Longitude = "+Double.toString(longitude),Toast.LENGTH_SHORT).show();
        Log.i("MainActivity","Lat = "+latitude+", lon = "+ longitude);
    }else{
        Toast.makeText(MainWeatherActivity.this, "No Permission. Grant Permission to continue", Toast.LENGTH_SHORT).show();
    }

}

编辑:     我忘了提到IntroActivity和MainActivity在清单文件中都有一个noHistory = true。

希望问题很明确......

2 个答案:

答案 0 :(得分:0)

启动活动的调用是异步的。您所看到的行为可能是因为这一点。

将第二个电话转到onActivityResult和第一个电话的else

if (firstStart) { 
    Intent i = new Intent(this,       MainIntroActivity.class);
    startActivityForResult(i, REQUEST_CODE_INTRO);
 } else {
    startActivity(new Intent(this, MainWeatherActivity.class));
}
finish();

答案 1 :(得分:0)

尝试替换以下代码:

   if (firstStart) {
        Intent i = new Intent(this, MainIntroActivity.class);
        startActivityForResult(i, REQUEST_CODE_INTRO);
    }else{
        startActivity(new Intent(this, MainWeatherActivity.class));
        finish();
    }