Take current Activity and put on another Activity

Asked

Viewed 322 times

0

My app has an Activity which is a customer list and also a sales Activity. When I only click on a name on the customer list it opens the customer details. There’s all the customer data. So far so good. But when I go to Activity sales I need to select the customer to make the sale. It turns out that when I click on the customer I would like to select only him and bring to the sales Activity. But when I click it opens the customer details screen.

Listview Clientes Cadastro de Produto

1 answer

3


I suggest passing a parameter to Activity of customers, to know what particular action, for example...

Call for details:

Intent intent = new Intent(activity, ActivityClientes.class);
Bundle bundle = new Bundle();
bundle.putInt("MODO_TELA", ActivityClientes.MODO_DETALHES);
intent.putExtras(bundle);
activity.startActivity(intent);

Call to select the customer:

private static final int IDENTIFICADOR_EXEMPLO = 0;

Intent intent = new Intent(activity, ActivityClientes.class);
Bundle bundle = new Bundle();
bundle.putInt("MODO_TELA", ActivityClientes.MODO_SELECIONAR);
intent.putExtras(bundle);
activity.startActivityForResult(intent, IDENTIFICADOR_EXEMPLO); 

in the same Activity implement:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IDENTIFICADOR_EXEMPLO) {
    if(resultCode == RESULT_OK) {
        Integer mIdSelect = data.getIntExtra("IdCliente", 0);
        //aqui você já tem o idSelecionado 
    }
  }
}

and in your customer’s Activity, treat the "current mode" for example:

public class ActivityClientes extends Activity{

   public static final int MODO_DETALHES = 0;
   public static final int MODO_SELECIONAR = 1;

   private int mModoAtual = -1;

   ....
   @Override
   public void onCreate(Bundle savedInstanceState) {

      Intent intent = getIntent();
      Bundle extras = intent.getExtras();
      if (extras != null) {
         if (extras.containsKey("MODO_TELA")) {
             mModoAtual = extras.getInt("MODO_TELA", -1);


    //você tera o "ModoAtual para usar na sua activity para tratar"
    //exemplo:

    if (mModoAtual == MODO_DETALHES){
      //código
    }

   }



   //quando quiser usar o activityForResult, seta o result antes de fechar a tela
   private void exemplo(){

    if (mModoAtual == MODO_SELECIONAR){
        Intent intent = new Intent();
        intent.putExtra("IdCliente", 5);
        setResult(RESULT_OK, intent);
    }

   }


  //por exemplo no onclick do item da sua listview, faça algo parecido com isso.
   public void OnClick(..){

    if (mModoAtual == MODO_DETALHES){
      //abrir detalhes
    }
    else if (mModoAtual == MODO_SELECIONAR){
        Intent intent = new Intent();
        intent.putExtra("IdCliente", idClienteSelecionado);
        setResult(RESULT_OK, intent);
        finish();
    }
   }

}
  • Eai Marco. Dear when I put this code in my main Activity Bundle.putInt("MODO_TELA", Activityclientes.MODO_DETALHES); putInt gives error. And he has no option to solve. You know what ?

  • @Arturmafezzolijúnior, did you in your Activity put the constant MODO_DETALHES? You can try replacing it with a fixed value to test, for example Bundle.putInt("MODO_TELA", 0)

  • can open the chat here. Because I still have doubts. =(

  • I’ll see how you open the chat

  • Just walk in here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.