gpt4 book ai didi

java - 如何将 Intent 传递回 MainActivity

转载 作者:行者123 更新时间:2023-12-02 09:49:54 24 4
gpt4 key购买 nike

我的应用程序在 MainActivity 中有一个 RecyclerView,其中包含一个 onClickListener,它将 Intent 发送到 DetailActivityDetailActivity 有一个不执行任何操作即可返回的按钮,以及一个向“Activity”发送 Intent 的按钮。我已经检查过这里的其他帖子,例如 How to pass intent with extras to an already running activity和其他人,但没有人结果。

这是我尝试过的版本:

主要 Activity :

@Override
protected void onNewIntent(Intent intent) {
int codPedido = getIntent().getIntExtra("CodPedido",1);

//Toast to test if it's working
Toast.makeText(getApplicationContext(), String.valueOf(codPedido), Toast.LENGTH_SHORT).show();
}

RecyclerViewAdapter的OnClick:

Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("CodigoPedido", pedidos.get(i).getCodPedido());
intent.putExtra("Local", pedidos.get(i).getLocal());
intent.putExtra("Dia",pedidos.get(i).getDia());
intent.putExtra("Periodo", pedidos.get(i).getPeriodo());
intent.putExtra("urgente", pedidos.get(i).isUrgente());
intent.putExtra("requisitante",pedidos.get(i).getRequisitante());
intent.putExtra("observacoes", pedidos.get(i).getObservacoes());

详细 Activity :

public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_confirm) {
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
intent.putExtra("CodPedido", codPedido);
startActivityForResult(intent, 1);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

编辑:

这是我现在尝试的:

    viewHolder.clRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity mActivity = new MainActivity();
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("CodigoPedido", pedidos.get(i).getCodPedido());
intent.putExtra("Local", pedidos.get(i).getLocal());
intent.putExtra("Dia",pedidos.get(i).getDia());
intent.putExtra("Periodo", pedidos.get(i).getPeriodo());
intent.putExtra("urgente", pedidos.get(i).isUrgente());
intent.putExtra("requisitante",pedidos.get(i).getRequisitante());
intent.putExtra("observacoes", pedidos.get(i).getObservacoes());
mActivity.startActivityForResult(intent, 1);
}
});

它在空对象引用上提供了方法“android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()”

我之前所做的是使用构造函数中的上下文

public RvPedidosAdapter(List<Pedido> pedidos, Context context) {
this.pedidos = pedidos;
this.context = context;
}

像这样:

    viewHolder.clRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("CodigoPedido", pedidos.get(i).getCodPedido());
intent.putExtra("Local", pedidos.get(i).getLocal());
intent.putExtra("Dia",pedidos.get(i).getDia());
intent.putExtra("Periodo", pedidos.get(i).getPeriodo());
intent.putExtra("urgente", pedidos.get(i).isUrgente());
intent.putExtra("requisitante",pedidos.get(i).getRequisitante());
intent.putExtra("observacoes", pedidos.get(i).getObservacoes());
context.startActivity(intent);
}
});

最佳答案

    On Your code please pass startActivityForResult() with intent param with result code and get the result on MainActivity in onActivityResult();

Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("CodigoPedido", pedidos.get(i).getCodPedido());
intent.putExtra("Local", pedidos.get(i).getLocal());
intent.putExtra("Dia",pedidos.get(i).getDia());
intent.putExtra("Periodo", pedidos.get(i).getPeriodo());
intent.putExtra("urgente", pedidos.get(i).isUrgente());
intent.putExtra("requisitante",pedidos.get(i).getRequisitante());
intent.putExtra("observacoes", pedidos.get(i).getObservacoes());
startActivityForResult(intent, 1);

On your second activity please use this code.
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
intent.putExtra("CodPedido", codPedido);
setResult(RESULT_OK, intent);
finish();

然后移至 MainActivity.class 以在 onActivityResult() 方法中获取结果数据,如下所示。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if(resultCode == RESULT_OK){
(data.getIntExtra("CodPedido");
}

}

关于java - 如何将 Intent 传递回 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56392982/

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