Home > Enterprise >  Data is not sending to the next activity using bundle in android studio
Data is not sending to the next activity using bundle in android studio

Time:05-27

This is Detail Activity... I am sending data from this activity to next activity using putExtra in intent

public class Detail_Activity extends AppCompatActivity {

    ImageView ivImage2;
    TextView txtDescription, txtName, txtPrice;
    String key = "";
    String imageUrl = "";
    Button delete_Recipe, update_Recipe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        ivImage2 = (ImageView)findViewById(R.id.ivImage2);
        txtDescription = (TextView) findViewById(R.id.txtDescription);
        txtName = (TextView) findViewById(R.id.txtName);
        txtPrice = (TextView) findViewById(R.id.txtPrice);
        delete_Recipe = (Button) findViewById(R.id.delete_Recipe);
        update_Recipe = (Button) findViewById(R.id.update_Recipe);

        update_Recipe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(Detail_Activity.this, Upload_Recipe.class)
                        .putExtra("recipeNameKey", txtName.getText().toString())
                        .putExtra("recipeDescriptionKey",  txtDescription.getText().toString())
                        .putExtra("recipePriceKey",  txtPrice.getText().toString())
                        .putExtra("oldimageUrl", imageUrl)
                        .putExtra("key",key)
             );
            }
        });

**This is the Update Activity... In this activity, data is not showing or don't show the received data, while the code is correct **

public class Update_Activity extends AppCompatActivity {

    ImageView update_Recipe_Image;
    EditText update_Recipe_Name, update_Recipe_Description, update_Recipe_Price;
    Button updatebutton;
    Uri uri;
    ActivityResultLauncher<String> mContent;
    String imageUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        update_Recipe_Image = (ImageView) findViewById(R.id.update_Recipe_Image);
        update_Recipe_Name = (EditText) findViewById(R.id.update_Recipe_Name);
        update_Recipe_Description = (EditText) findViewById(R.id.update_Recipe_Description);
        update_Recipe_Price = (EditText) findViewById(R.id.update_Recipe_Price);
        updatebutton = (Button) findViewById(R.id.updatebutton);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null){
            Glide.with(Update_Activity.this)
                    .load(bundle.getString("oldimageUrl"))
                    .into(update_Recipe_Image);
            update_Recipe_Name.setText(bundle.getString("recipeNameKey"));
            update_Recipe_Description.setText(bundle.getString("recipeDescriptionKey"));
            update_Recipe_Price.setText(bundle.getString("recipePriceKey"));

        }
    }
}

There is no any error in the logcat.

CodePudding user response:

startActivity(new Intent(Detail_Activity.this, Upload_Recipe.class)
                        .putExtra("recipeNameKey", txtName.getText().toString())

Try creating the intent with the .putExtra before you put it as a parameter for startActivity. If you want to pass the data to update activity you need to give the destination of intent to that activity Like:

Intent myIntent= new Intent(Detail_Activity.this, Update_Activity.class)
                        .putExtra("recipeNameKey", txtName.getText().toString())
startActivity(myIntent)

CodePudding user response:

You are not passing data in bundle, you are directly passing data using intent. So you can receive your data in next screen like this:

 if (getIntent() != null){
            Glide.with(Update_Activity.this)
                    .load(getIntent().getStringExtra("oldimageUrl"))
                    .into(update_Recipe_Image);
            update_Recipe_Name.setText(getIntent().getStringExtra("recipeNameKey"));
            update_Recipe_Description.setText(getIntent().getStringExtra("recipeDescriptionKey"));
            update_Recipe_Price.setText(getIntent().getStringExtra("recipePriceKey"));

        }

Just need to replace bundle.getString with getIntent().getStringExtra in your Update_Activity

CodePudding user response:

Note there are 3 activities here. Detail_Actiivty. Upload_Recipe (is it activity?), Update_Activity. You should create Intent with the destination class.

Intent intent = new Intent ( DetailActivity.this, Update_Activity.class) ....
  • Related