0

我有一个具有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 =清单文件真实..

希望问题很清楚...

+0

尝试删除完成();已经尝试过 – YoLo

+0

。不用找了.. –

回答

0

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

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

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

尝试更换以下代码:

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