Take current activity and put in another activity

0

My app has an activity that is a list of clients and also a sales activity. When I click only on a name in the client list it opens the client details. There you have all the customer data. So far so good. But when I go into the sales activity I need to select the customer to make the sale. It turns out that when I click on the client I would just select it and bring it to the sales activity. But when I click it it opens the customer details screen.

    
asked by anonymous 23.09.2016 / 13:16

1 answer

3

I suggest passing a parameter to the client activity, to know which 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 client:

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 deployment activity:

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

In your client 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();
    }
   }

}
    
23.09.2016 / 13:42