Two Intents starting the same Activity

Asked

Viewed 83 times

1

I have a button to include a Hint and I have another button to edit the Hint already created. I would like to use the same creation activity, when it is for editing would pull the data already created. I’m in doubt how to do the Intent code, I’m not getting it. It’s not pulling the data. I’m trying it this way:

{ Intent novadica = getIntent();
        if (novadica==null){

        enviardica.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                ParseObject dica = new ParseObject("Dicas");



                dica.put("nome", edttitulodica.getText().toString());
                dica.put("conteudoDica", edtconteudodica.getText().toString());
                dica.put("resumo", resumoChamadaDica.getText().toString());



                dica.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {


                        Toast.makeText(Inclusaodedica.this, "Dica enviada com sucesso.", Toast.LENGTH_LONG).show();

                        startActivity(new Intent(getApplicationContext(), MainActivity.class));
                    }
                });


            }
        });}else{

            Intent editardica = getIntent();
          String rdicaObjectId = editardica.getStringExtra("dicaObjectId");

            final ParseQuery <ParseObject> dica = new ParseQuery<ParseObject>("Dicas");
            dica.getInBackground(rdicaObjectId, new GetCallback<ParseObject>() {
                @Override
                public void done(final ParseObject object, ParseException e) {


                    edttitulodica.setText(object.getString("nome"));
                    edtconteudodica.setText(object.getString("conteudoDica"));
                    resumoChamadaDica.setText(object.getString("resumo"));

                    enviardica.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {



                            object.put("nome", edttitulodica.getText().toString());
                            object.put("conteudoDica", edtconteudodica.getText().toString());
                            object.put("resumo", resumoChamadaDica.getText().toString());


                            object.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(ParseException e) {


                                    Toast.makeText(Inclusaodedica.this, "Dica alterada com sucesso.", Toast.LENGTH_LONG).show();

                                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                                }
                            });





                        }
                    });

Can someone help me?

  • We need a more detailed description of the problem. What is "not pulling the data"? Is there an error? Some line that was supposed to give a result gives another?

  • Hi Pablo is the following: I have an Activity - Includica, and another Activity - Tip (with an edit button). The Includica Activity consists of empty Edittexts. Activity Tip, when you click the edit button - I wanted you to open Activity Inclutip with the Hint data in Edittexts. But you’re not pulling the dice.

  • Okay, but the mistake?

  • the error is that it is not bringing the data from Intent editardica

  • How did you find out you’re not bringing?

  • By the simulator.

  • It would be easier for you to pass information by Bundle. A Boolean, for example, indicating whether the Intent is coming from the edit or add button.

Show 2 more comments

1 answer

2


I would pass a Extra Beta to another Activity:

Instead of:

startActivity(new Intent(getApplicationContext(), MainActivity.class));

Do:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("is_new", true);
startActivity(intent);

In the other Activity, confirm:

Bundle extras = getIntent().getExtras();
if(extras != null){
   if(extras.getBoolean("is_new", false)){
       //se a váriavel "is_new" estiver como true entra aqui
   }else{
       //se ela estiver false, ou seja, é edição, entra aqui
   }
}
  • Hi. Perfect worked. Thank you very much!!

  • I’m glad it worked! I forgot to mention, but the false there in the second Activity, it is a default value if the value of the previous Activity comes empty.

Browser other questions tagged

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