Activity stack not working as it is supposed to

41 views Asked by At

I have an app which has a MainActivity.

If its first launch, it launches an activity which displays a intro slider and if its not, it launches a MainWeatherActivity.

Here is the code from the 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();
        }
    }
}

When I open the app for the first time , user is supposed to see the MainIntroActivity and then the MainWeatherActivity. But instead this code directly launches the MainWeatherActivity and when I press the back button it launches the MainIntroActivity.

Where have I gone wrong and How do I fix this?

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();
    }

}

EDIT : I forgot to mention that both IntroActivity and MainActivity has a noHistory=true in the manifest file..

Hope the question is clear...

2

There are 2 answers

0
BMacedo On

The call for starting activities is asynchronous. The behaviour you are seeing might be because of that.

Move the second call to the onActivityResult and to the else of the first if.

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

try replacing following code:

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