답변:
onBackPressed()메서드 를 재정의하고 수퍼 클래스를 호출 하기 전에 결과 를 설정 해야 합니다.
@Override
public void onBackPressed() {
Bundle bundle = new Bundle();
bundle.putString(FIELD_A, mA.getText().toString());
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
super.onBackPressed();
}
Activity를 호출 하기 전에 결과를 설정해야합니다 finish(). 클릭 BACK 실제로 호출 finish()당신에 activity당신이 다음 코드를 사용할 수 있도록 :
@Override
public void finish() {
Intent data = new Intent();
setResult(RESULT_OK, data);
super.finish();
}
당신이 호출 할 경우 NavUtils.navigateUpFromSameTask();에 onOptionsItemSelected(), finish()라고,하지만 당신은 잘못을 얻을 것이다 result code. 그래서 당신은 호출 할 필요가 finish()없습니다 navigateUpFromSameTask에 onOptionsItemSelected().
onActivityResult의 잘못된 requestCode
finish함께 할 수있다 onPause와 onDestroy? 사람들은 완전히 그 위해를 제외하고 관련이없는 finish종료 프로세스를 시작 onPause하고 onDestroy의 일부를.
내 코드를 리팩토링했습니다. 처음에 나는 일부 데이터를 준비로 설정 activity result에서 onDestroy(이 작동하지 않았다). 이제 activity반환 할 데이터가 업데이트 될 때마다 데이터를 설정 하고 onDestroy.
setResult()데이터가 손실됩니다.
다음 과 같이 onOptionsItemSelected 를 재정의해야 합니다.
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
final Intent mIntent = new Intent();
mIntent.putExtra("param", "value");
setResult(RESULT_OK, mIntent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
참조 하여 onActivityResult (INT, INT, 의도) 문서를
해결책은 Activity.RESULT_CENCELED 값에 대한 resultCode 를 확인하는 것 입니다. 그렇다면 BACK을 누르거나 활동이 중단되었음을 의미합니다. 그것이 당신들에게 효과가 있기를 바랍니다.
onDestroy체인에서 너무 늦었습니다. 대신 활동이 수명주기가 끝났는지 onPause확인 isFinishing()하기 위해 재정의 하고 확인하십시오 .
onBackPressed (안드로이드 레벨 5 이상)를 재정의하거나 onKeyDown ()을 재정의하고 KeyEvent.BUTTON_BACK을 잡으십시오 ( Android Activity Results 참조 ) 이것은 나를 위해 트릭을 수행합니다.
초기 활동으로 돌아갈 때 한 활동의 onPause에서 실행되는 논리에 의존하지 마십시오. 문서에 따르면 :
이 메서드 (onPause)의 구현은이 메서드가 반환 될 때까지 다음 활동이 재개되지 않으므로 매우 빠릅니다.
자세한 내용은 http://goo.gl/8S2Y 를 참조하세요.
가장 안전한 방법은 각 결과 변경 작업이 완료된 후 결과를 설정하는 것입니다 (답변에서 언급했듯이)
대답을 붙여 넣으면 다른 사람들에게 도움이 될 수 있습니다. android : launchMode = "singleTask"를 사용하여 launcheMode를 설정하면 결과를 얻을 수 없습니다.
/* <p>Note that this method should only be used with Intent protocols
* that are defined to return a result. In other protocols (such as
* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
* not get the result when you expect. For example, if the activity you
* are launching uses the singleTask launch mode, it will not run in your
* task and thus you will immediately receive a cancel result.
*/
과:
/* <p>As a special case, if you call startActivityForResult() with a requestCode
* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
* activity, then your window will not be displayed until a result is
* returned back from the started activity. This is to avoid visible
* flickering when redirecting to another activity.
*/