2017-10-18 119 views
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(); 
    }