我有2项活动,家长活动开始儿童活动。当我们进入Child Activity时,用户通过菜单中的“Up”-Button关闭它,并返回到Parent Activity。
基本代码摘要:
家长(主要)活动:
protected static final int TIMERCHOOSER_REQUEST = 1;
开始新活动(从菜单按钮调用):
private void openTimerChooser() {
Intent intent = new Intent(this, TimerChooserActivity.class);
startActivityForResult(intent,TIMERCHOOSER_REQUEST);
}
并获取结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
Log.d("Main", "called onActivityResult"); //this and below is never executed
if (requestCode == TIMERCHOOSER_REQUEST) {
switch(resultCode) {
case RESULT_CANCELED:
Log.d("Main", "Intent result was canceled by TIMERCHOOSER");
case RESULT_OK:
//code to handle data from TIMERCHOOSER_REQUEST
Log.d("Main", "accepted resultCode. value: "+resultCode);
String result = data.getStringExtra("result");
Toast toast = Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT);
toast.show();
default:
Log.d("Main", "invalid/unhandled resultCode? value: "+resultCode);
}
}
else
Log.d("Main", "invalid/unhandled requestCode? value: "+requestCode);
}
子活动(“TimerChooserActivity”):
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
case R.id.action_settings:
return true;
case android.R.id.home:
Log.d("TimerChooser", "finishing intent");
Intent returnIntent = new Intent();
returnIntent.putExtra("result", "Some result");
/*if(result == "User activated textview")
setResult(RESULT_OK,returnIntent);
else
setResult(RESULT_CANCELED,returnIntent);*/
setResult(RESULT_OK,returnIntent);
finish();
default:
return super.onOptionsItemSelected(item);
}
}
您可以通过菜单/操作栏中的主页/向上按钮返回到Parent(Maint)活动。这有效。但是,无论我设置什么样的结果,都不会调用onActivityResult()
。
我的清单中也没有奇怪的标签。只是标准的东西:
android:allowBackup
android:icon
android:label
android:theme
android:name
android:parentActivityName
android:value
发生了什么事?
答案 0 :(得分:1)
在你的android.R.id.home
案例中,你没有返回一个值,而是你正在进入默认情况