如何将意图传递回MainActivity

时间:2019-05-31 10:06:57

标签: java android android-intent

我的应用在RecyclerView中有一个MainActivity,并且onClickListener将意图发送到DetailActivityDetailActivity有一个按钮,可以不执行任何操作而返回,还有一个按钮,用于向“活动”发送意图。 我在这里检查了How to pass intent with extras to an already running activity等其他帖子,但没有结果。

这是我尝试过的版本:

MainActivity:

@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());

DetailActivity:

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);
        }
    });

给出method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference

我之前在上下文中使用的是构造器中的内容

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);
        }
    });

2 个答案:

答案 0 :(得分:0)

您可以使用 startActivityForResult(Intent intent,int requestcode);

请参阅此帖子-> startActivityForResult

答案 1 :(得分:0)

    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");          
         }

}