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を起動し、私がBackボタンを押すと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(); 
    } 

} 

EDIT: 私はIntroActivityとMainActivityの両方がマニフェストファイルの真noHistory =を持っていることを言及するのを忘れてしまった...

が質問ホープはっきりしています...

+0

)(仕上げを削除してみてください。 – YoLo

+0

は既に試しました。変化なし.. –

答えて

0

活動を開始する呼び出しは非同期です。あなたが見ている行動はそのためかもしれません。

onActivityResultと最初のifのelseに2番目の通話を移動します。

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(); 
    } 
関連する問題