我的MainActivity包含一个由两个选项卡组成的片段,每个选项卡中有一个片段(ChatListFragment和FriendListFragment)。每个片段都有ListView。当用户点击ListView的每一行时,将显示ChatActivity。
我希望ChatListFragment中的ListView(使用ListAdapter作为调用ChatActivity的适配器)在ChatActivity关闭后更新其ListView。我在我的代码中尝试了onActivityResult方法,如下所示,但requestCode始终为-1。
MainActivity.java
public class MainActivity extends AppCompatActivity {
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: " + " " + requestCode + " == " + MYACTIVITY_REQUEST_CODE + " and " + resultCode + " == " + Activity.RESULT_OK);
Log.d(TAG, "onActivityResult: " + MainActivity.class.getSimpleName());
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
...
}
ChatListFragment.java
public class ChatListFragment extends Fragment {
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: " + TAG);
Log.d(TAG, "onActivityResult: " + " " + requestCode + " == " + MYACTIVITY_REQUEST_CODE + " and " + resultCode + " == " + Activity.RESULT_OK);
if ((requestCode == MYACTIVITY_REQUEST_CODE) && (resultCode == Activity.RESULT_CANCELED)) {
List<Friend> friendList = getFriendChatList();
adapter = new ListAdapter(getActivity(), R.layout.fragment_list, friendList, true);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
Log.d(TAG, "onActivityResult number of friend chats: " + getFriendChatList().size());
if(friendList.isEmpty()) {
linearLayout.setVisibility(View.VISIBLE);
} else {
linearLayout.setVisibility(View.GONE);
}
}
}
}
ChatActivity.java
public class ChatActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (getParent() == null) {
setResult(Activity.RESULT_OK);
} else {
getParent().setResult(Activity.RESULT_OK);
}
}
...
}
ListAdapter.java
public class ListAdapter extends ArrayAdapter<Friend> {
private static final int MYACTIVITY_REQUEST_CODE = 9000;
public ListAdapter(Context context, int resource, List<Friend> objects, boolean isChat) {
super(context, resource, objects);
this.context = context;
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(null == view) {
view = layoutInflater.inflate(R.layout.list_friend, null);
}
final Friend friend = friendList.get(position);
if(null != friend) {
final CardView rowLayout = (CardView) view.findViewById(R.id.row_layout);
final ImageView friendImage = (ImageView) view.findViewById(R.id.list_friend_image);
final TextView friendName = (TextView) view.findViewById(R.id.list_friend_name);
final TextView friendDescription = (TextView) view.findViewById(R.id.list_friend_description);
final TextView friendLastChatTime = (TextView) view.findViewById(R.id.list_friend_lastChatTime);
// Open chat activity
rowLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(KEY_FRIEND_NAME, friend.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) context).startActivityForResult(intent, MYACTIVITY_REQUEST_CODE);
}
});
}
return view;
}
}
答案 0 :(得分:1)
您需要创建一个Intent,然后将Result设置为该Intent。
在ChatActivity
Intent intent=new Intent();
if (getParent() == null) {
setResult(Activity.RESULT_OK,intent);
} else {
getParent().setResult(Activity.RESULT_OK,intent);
}
finish();
设置结果后,您还需要finish
您的ChatActivity。像这样:
我不明白,这就是为什么你需要检查父母班级。你总是可以这样做
Intent intent= new Intent();
setResult(RESULT_OK,intent);
finish();
这应该正常工作。
希望这会有所帮助:)
答案 1 :(得分:0)
我知道这真的很晚!但是,onActivityResult
将不会获得您在setResult()
中设置的结果!
因为您在Intent.FLAG_ACTIVITY_NEW_TASK
rowLayout.setOnClickListener
当呼叫者从以下位置请求结果时,不能使用此标志 正在启动的活动。
因此,首先删除intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
然后setResult(RESULT_OK,intent);
放在适当的位置
在这种情况下,您将获得在onActivityResult