我在片段中獲得了一個片段中的列表,該片段啓動了一個detailactivity,其中包含片段,其中包含所選對象的信息。片段在onActivityResult中沒有收到正確的結果
如果選擇了其中一個操作項目,則活動的結果爲OK,如果再次選擇,則結果切換回CANCELED。我只想在用戶導航返回列表時傳遞此結果。 我已經消除了幾個問題,我到達列表片段中的onActivityResult函數,但結果代碼不是我通過的。 在調試時,我也注意到listactivity中的onActivityResult函數從未觸發。
ListActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("LA", "onActivityResult: " + resultCode);
super.onActivityResult(requestCode, resultCode, data);
}
ListFragment:
/**
* Launch detail activity
*/
public class OnObjectClickListener implements OnItemClickListener {
public OnObjectClickListener() {
super();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("OnObjectClickListener", "Selected an object");
ObjectAdapter adapter = (ObjectAdapter) _objList.getAdapter();
Object o = adapter.getItem(position);
Intent i = new Intent(adapter.getContext(), DetailActivity.class);
i.putExtra(DetailActivity.ARG_OBJCODE, o.getCode());
startActivityForResult(i, DetailActivity.ARG_FOLLOW_OBJ_CHANGE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("LF", "Activity result received");
if (requestCode == DetailActivity.ARG_FOLLOW_OBJ_CHANGE
&& resultCode == Activity.RESULT_OK
&& data != null
&& data.hasExtra(DetailActivity.ARG_OBJCODE)){
// Update object in the list: following indicator has changed
String objcode = data.getStringExtra(DetailActivity.ARG_OBJCODE);
ObjectAdapter oa = (ObjectAdapter) _objList.getAdapter();
for (Object o : oa.getObjects()){
// Toggle following state in the game
if (o.getCode().equals(objcode)){
o.setFollowing(!o.isFollowing());
oa.notifyDataSetChanged();
Log.i("LF", "Edited dataset");
break;
}
}
Log.i("LF", "Result processed");
} else {
super.onActivityResult(requestCode, resultCode, data);
Log.i("LF", "Result not recognized");
}
}
行動項目詳細的活動:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
DetailFragment df = (DetailFragment) getFragmentManager().findFragmentByTag(_objCode);
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_follow_obj:
if (df != null){
df.followObj(true);
configResult();
}
break;
case R.id.action_unfollow_obj:
if (df != null){
df.followObj(false);
configResult();
}
break;
}
return super.onOptionsItemSelected(item);
}
/** Sets the result of this activity (did the following parameter change, or not?)
* @return nothing */
private void configResult() {
// switch follow state monitor: statechange is true upon uneven changes
// meaning the state is not really changed if the game is first followed and subsequently unfollowed
_followStateChanged = !_followStateChanged;
Log.i("DA", "State changed: " + _followStateChanged);
}
這是我試圖得到的結果回到先前的listfragment:
@Override
public void onBackPressed() {
super.onBackPressed();
// config result
Intent i = new Intent();
i.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
i.putExtra(DetailActivity.ARG_OBJCODE, _objCode);
int result = _followStateChanged ? Activity.RESULT_OK : Activity.RESULT_CANCELED;
if (getParent() == null){
setResult(result, i);
} else {
getParent().setResult(result, i);
}
this.finish();
Log.i("DA", "Back pressed, intent passed and set as a result");
}
...的getParent()總是空
的resultCode爲始終爲0,沒有任何人有一個想法,我在哪裏呢? 我已經諮詢了這些問題的答案,應用和測試他們,得到了進一步的但沒有辦法解決尚未:
- onActivityResult not being called in fragment
- Call fragments via super in host activity#onActivityResult
- General approach I took
- Verify method used
- Override and pass to fragment
代替使用getParent與活動進行通信,您應該使用接口並創建一個偵聽器。 – Martin 2014-11-24 00:17:38
你的意思是與以前的活動溝通? getParent在第二個細節活動中使用,但if結構的else括號永遠不會被調用......您仍然會這樣做嗎?這是獲得反饋的另一種方式,想要使用結果。 – 2014-11-24 09:25:39
無論哪種方式。與父母片段的交流應該經常通過像@Martin提到的聽衆。 – QuirijnGB 2014-11-25 10:21:17