我从ProfileActivity
工具栏开始HomeActivity
。
出于某种原因,当我从ProfileActivity
返回时,onActivityResult()
未被调用。以下是相关代码。提前谢谢!
HomeActivity
内的工具栏代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.searchBox:
return true;
case R.id.userProfile:
Intent intent = new Intent(HomeActivity.this, ProfileActivity.class);
Bundle b = new Bundle();
b.putString("userID", uID);
intent.putExtras(b);
startActivityForResult(intent, 10);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
onActivityResult()
内的 HomeActivity
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
Log.d("WorkoutLog", "onresult called");
if(resultCode == Activity.RESULT_OK){
Toast.makeText(context, data.getStringExtra("date"), Toast.LENGTH_SHORT).show();
}
}
}
ProfileActivity
中调用的方法返回主页:
public void editWorkout(String date){
Intent intent = new Intent();
intent.putExtra("date", date);
setResult(Activity.RESULT_OK, intent);
finish();
}
HomeActivity
和ProfileActivity
清单:
<activity
android:name=".HomeActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ProfileActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.PROFILE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 0 :(得分:3)
finish()
有效杀死了调用Activity
,请勿在{{1}}之后调用它:
startActivityForResult()