2016-07-22 39 views
1

我使用请求码从MainActivity调用到InfomationActivity。但是,当返回MainActivity时,它不活动。这里有什么问题?ResultCode和RequestCode不起作用

MainActivity,使用requestcode叫InfomationActivity

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

InfomationActivity,返回一个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回报:

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

在logcat的,我当resultcode = 3时看不到它运行。为什么当resultcode = 3时,它不会运行?

+0

与4,很好。 –

+0

整数值不适用于此结果,它可能表现不正常,所以我建议您仅使用RESULT_OK并将其中的一个参数设置为3或4作为整数,主要的是您不需要将所有值都放入如果 - 其他循环,所以试试 – Vickyexpert

+0

如果有2个案件处理,它会好的,但我想更多的情况下? –

回答

1

结果代码应该使用预先设定的值RESULT_OK,RESULT_CANCELLEDRESULT_FIRST_USER中的一个在启动的活动中进行设置。

像3和4这样的设置值在这里不一定有定义的行为;如果他们对你有一些意义(并且不是随机的),那么将它们作为另一个额外的意图传递给Intent。使用RESULT_OK替代。

此外,对于您用于传回结果的意图,我建议使用new Intent()创建一个新的,而不是重复使用用于启动活动的那个(这是getIntent()为您提供的那个)。

在任何情况下,我建议您查看this section of the docs,其中涉及开始活动和获得结果。

+0

我需要返回4处理案件,我该怎么办? –

+0

@KhanhDoan,如果你需要处理4种不同的情况,那么我建议你设置结果代码为RESULT_OK,然后通过int来指定它在你的返回Intent中的用例。然后在你的调用活动中,你检查返回意图,看看它是4个案例中的哪一个。 –

相关问题