2016-07-22 4 views
1

リクエストコードを使用してMainActivityからInfomationActivityに電話しました。しかし、MainActivityを返すとき、それは非アクティブです。ここでの問題は何ですか? MainActivityResultCodeとRequestCodeが機能しない

、requestcodeを使用してInfomationActivityを呼び出す:InfomationActivity

Intent intent = new Intent(MainActivity.this, InfomationActivity.class); 
startActivityForResult(intent, 100); 

、ResultCodeのを返す:

 if(btnAlarmInfo.getVisibility() == View.VISIBLE){ 
      //run 
      Log.d("abc", note.getTitle() + "/" + note.getNote() + "/" + note.getDateTime() + "/" + note.getColorBackground()); 
      Log.d("abc", Integer.toString(images.size())); 
      Intent intent = getIntent(); 
      intent.putExtra("title", note.getTitle()); 
      intent.putExtra("note", note.getNote()); 
      intent.putExtra("time", note.getDateTime()); 
      intent.putExtra("color", note.getColorBackground()); 
      intent.putParcelableArrayListExtra("image", images); 
      setResult(3, intent); 
      finish(); 
     }else{ 
      //run 
      Intent intent = getIntent(); 
      intent.putExtra("title", note.getTitle()); 
      intent.putExtra("note", note.getNote()); 
      intent.putExtra("time", note.getDateTime()); 
      intent.putExtra("color", note.getColorBackground()); 
      intent.putExtra("day", note.getDayAlarm()); 
      intent.putExtra("hour", note.getHourAlarm()); 
      intent.putParcelableArrayListExtra("image", images); 
      setResult(4, intent); 
      finish(); 
     } 

いつMainActivityリターン:logcatで

if(requestCode == 100){ 
     if(resultCode == 3){ 
      //not run ???????? 
      Log.d("abc", "it's me"); 
      String title = data.getExtras().getString("title"); 
      String note = data.getExtras().getString("note"); 
      String time = data.getExtras().getString("time"); 
      String color = data.getExtras().getString("color"); 
      ArrayList<Image> image = data.getParcelableArrayListExtra("image"); 
      Log.d("abc", Integer.toString(image.size())); 
      ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(); 
      for(int i = 0; i < image.size(); i++){ 
       bitmaps.add(image.get(i).getImage()); 
      } 
      Note note1 = new Note(title, note, false, time, color, "", "", bitmaps); 
      this.addNote(note1); 
     }else if(resultCode == 4){ 
      //run 
      String title = data.getExtras().getString("title"); 
      String note = data.getExtras().getString("note"); 
      String time = data.getExtras().getString("time"); 
      String color = data.getExtras().getString("color"); 
      String day = data.getExtras().getString("day"); 
      String hour = data.getExtras().getString("hour"); 
      ArrayList<Image> image = data.getParcelableArrayListExtra("image"); 
      ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(); 
      for(int i = 0; i < image.size(); i++){ 
       bitmaps.add(image.get(i).getImage()); 
      } 
      Note note1 = new Note(title, note, true, time, color, day, hour, bitmaps); 
      this.addNote(note1); 
     } 
    } 

、私はresultcode = 3のときに実行されないことを確認してください。 resultcode = 3のときは実行されませんか?

+0

と4、それはいいです。 –

+0

整数値はこの結果には適用されません。異常な動作をする可能性がありますので、RESULT_OKのみを使用し、3または4をintとして1つのパラメータを設定することをお勧めします。 if-elseループので試してみてください – Vickyexpert

+0

2つのケースが処理されていればOKですが、もっとケースが必要ですか??? –

答えて

1

結果コードは、起動されたアクティビティで定義済みの値RESULT_OK,RESULT_CANCELLED、またはRESULT_FIRST_USERのいずれかを使用して設定されています。

3や4などの設定値は、ここで定義された動作を必ずしも持っているとは限りません。彼らがあなたのために何らかの意味を持っているならば(そしてランダムではありません)、それらをインテントのもう一つの余分なものとして渡してください。代わりにRESULT_OKを使用してください。

また、結果を返すインテントでは、アクティビティの開始に使用されたものを再利用するのではなく、new Intent()を使用して新しいものを作成することをお勧めします(これはgetIntent()です)。

いずれにしても、アクティビティの開始と結果の取得を扱うthis section of the docsを確認することをおすすめします。

+0

私は4ハンドルケースが返される必要があります、私は何をすべきですか? –

+0

@KhanhDoan、あなたが処理する必要がある4つの異なるケースがある場合は、RESULT_OKとして結果コードを設定してから、あなたのリターンインテントにあるユースケースを指定するintを渡すことをお勧めします。その後、あなたの呼び出しアクティビティでは、4つのケースのうちどれがそれであるかを確認するためにリターンのインテントをチェックします。 –

関連する問題