How to send data between Activitys without opening another window? Android Studio

Asked

Viewed 427 times

1

Good afternoon gentlemen, I have a question in my code, I need to pass the data from one Activity to another but when I put the value it opens the same screen twice, it is working but when a value is placed in the field of Activity it opens another screen but with the value informed, Could you help me ? Follow my code below:

Activity I am sending :

public class NovaCor extends AppCompatActivity {

public Button botao1;
botao1 = (Button) findViewById( R.id.btn1);

public void botao1(View v)
    {
        if (bool == false) {
            //String str1 = this.botao1.getBackground().toString();
            ColorDrawable buttonColor = (ColorDrawable) this.botao1.getBackground();
            int colorInt = buttonColor.getColor();
            String colorHex = Integer.toHexString(colorInt);
            Intent intent = new Intent(getApplication(), CriarCateg.class);
            intent.putExtra("KEY", colorHex);
            this.startActivity(intent);
            this.finish();
        }
    }

}

Activity I am getting:

public class CriarCateg extends AppCompatActivity {
private EditText descricao;
public EditText cor;
public Button altera;

 public void onCreate(@Nullable Bundle savedInstanceState) {

        descricao = (EditText) findViewById( R.id.textViewDescrCateg);
        cor = (EditText) findViewById( R.id.txtviewcorcateg);
        altera = (Button) findViewById( R.id.alterarcor);

        Bundle bundle = this.getIntent().getExtras();
        if (bundle != null){
            String teste =  bundle.get("KEY").toString();
            this.cor.setText("#" + teste);
            altera.setBackgroundColor(Color.parseColor("#"+teste));



        }
    }
  • When you click the button you just want to pass the Bundle and not access the other Activity?

  • explain better what you want to do, got a little confused

  • 1

    So I have a screen and when I click on the "Create Category" button it starts a new Activity with a new layout, within the "Create Category" has another button called "Select the color" which starts another Activity with another layout and with a color palette. When a color is selected it opens again the "Create Category" Activity with the selected color, that is, it keeps the Activity "create category" open twice. What I wanted is that instead of him opening up again, he filled in what’s open, but my tests didn’t work.

  • resolved with my reply???

1 answer

1


Hello, in your Activity Criacateg, in the button that calls Activity Novacor, do the following:

            Intent intent = new Intent(getApplication(), NovaCor.class);
            //inicia a activity desejada, enquanto está continua aberta esperando
            // um resultado
            startActivityForResult(intent, 1);

Still inside Criacateg, but outside onCreate, create the method that will receive the result:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == 1) {

        String teste =  data.getStringExtra("KEY");
        this.cor.setText("#" + teste);
        altera.setBackgroundColor(Color.parseColor("#"+teste));
         }
    }

In the Activity Novacor button click method, leave:

public void botao1(View v){
        if (bool == false) {
            ColorDrawable buttonColor = (ColorDrawable) this.botao1.getBackground();
            int colorInt = buttonColor.getColor();
            String colorHex = Integer.toHexString(colorInt);
            Intent intent = new Intent();
            intent.putExtra("KEY", colorHex);
            this.setResult(RESULT_OK, intent);
            this.finish();
        }
    }

EDIT 1:

You can also add if the user presses the back button in Activity Novacor.

Inside the New Color Activity, outside the onCreate, call the onBackPressed method and let:

public void onBackPressed() {
    super.onBackPressed();

    Intent intent = new Intent();
    setResult(RESULT_CANCELED, intent);
    finish();
}

And in the onActivityResult method in Activity Criacateg, after the if condition you can put:

  else {
        Toast.makeText(getApplicationContext(),"Nenhuma cor selecionada" , Toast.LENGTH_LONG).show();
    }
  • 1

    If the answer helped you, please mark it as answered and/or useful.

Browser other questions tagged

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