gpt4 book ai didi

java - Adapter中startActivityForResult返回时获取onActivityResult

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:06 27 4
gpt4 key购买 nike

当用户从 Activity 中点击返回时,我试图返回一些数据。

我正在从适配器调用 startActivityForResult,但是当我实际从 Activity 按下时,onActivityResult 似乎从未被调用

例如,代码被精简

fragment 1

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View mainView = inflater.inflate(R.layout.grid_view, container, false);

return mainView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

mAdapter = new Adapter(this.getActivity(), new ArrayList<Item>());
mGridView = (StaggeredGridView) getView().findViewById(R.id.grid_view);
mGridView.setAdapter(mAdapter);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.w(TAG, "in activity result");
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
}

适配器

public Adapter(Context context, List<Item> objects) {
super(context, -1, objects);
this.context = context;
addAll(objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.grid_view_item, parent, false);
}

View recommendationLayout = view.findViewById(R.id.recommendation_layout);
recommendationLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), Activity.class);
Bundle extras = new Bundle();
extras.putBoolean("denyEditPermission", true);
intent.putExtras(extras);

((Activity) context).((Activity) context).startActivityForResult(intent, 1);(intent, 1);
}
});
return view;

Activity

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detailed_view);

Intent intent = getIntent();
Bundle extras = intent.getExtras();
denyEditPermission = extras.getBoolean("denyEditPermission");
}

@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("denyEditPermission", denyEditPermission);
setResult(RESULT_OK, intent);
finish();
}

最佳答案

由于您使用 activity 上下文启动 Activity ,您将在 Activity 中获得 onActivityResult 回调,而不是在 fragment 中。要在 fragment 中获得回调,您需要在 fragment 本身中使用 fragment 上下文/启动 Activity 。我建议使用接口(interface)方法来解决这里的问题。创建一个接口(interface)并在您的 fragment 中实现它,当单击 recommendationLayout 时,从适配器调用接口(interface)方法,以便您可以处理 fragment 中的点击事件本身。

有点像,

创建接口(interface)文件-

public interface RecommendationClickListener {

public void onRecommendationClicked();
}

fragment :

public class MyFragment extends Fragment implements RecommendationClickListener{
...
...
...

@Override
public void onRecommendationClicked() {
Intent intent = new Intent(getActivity(), NextActivity.class);
Bundle extras = new Bundle();
extras.putBoolean("denyEditPermission", true);
intent.putExtras(extras);

startActivityForResult(intent, 1);

}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

mAdapter = new Adapter(this.getActivity(), new ArrayList<Item>());
mAdapter.setRecommendationClickListener(this);
mGridView = (StaggeredGridView) getView().findViewById(R.id.grid_view);
mGridView.setAdapter(mAdapter);
}

适配器:

private RecommendationClickListener mRecommendationClickListener;


public void setRecommendationClickListener(RecommendationClickListener recommendationClickListener) {
this.mRecommendationClickListener = recommendationClickListener;
}

View recommendationLayout = view.findViewById(R.id.recommendation_layout);
recommendationLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mRecommendationClickListener != null) {
mRecommendationClickListener.onRecommendationClicked();
}
}
});

关于java - Adapter中startActivityForResult返回时获取onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com