0

enter image description here
I want to fetch the data from previous activity (using getIntent) in a .java class and same the data to next activity through intent. can anyone help me how.Thanks in advance.

public class AddToCartHelper {

    public static void addToCart(Context context, Intent intent) {

        String TAG = "AddToCart";
        DecimalFormat decimalFormat;
        boolean loginflagforuser = false, loginflagforguest = false;

        String advId = "", num = "", uid = "", productName = "", emailCart = "",cartMessage = "";
        Double price = 0.0;
        Integer quantity = 1;

        SharedPreferences preferences = context.getSharedPreferences("SECRETFILE", Context.MODE_PRIVATE);

        loginflagforuser = preferences.getBoolean(Parameters.userEmail, false);
        loginflagforguest = preferences.getBoolean(Parameters.guestEmail, false);
        decimalFormat = new DecimalFormat("##.##");

        if (loginflagforuser){

            Intent fromCart = getIntent();
            //    imageId = fromCart.getStringExtra("image_url");
            advId = fromCart.getStringExtra("Advid");
            price = fromCart.getDoubleExtra("price", 0.0);
            num = fromCart.getStringExtra("num");
            uid = fromCart.getStringExtra("uid");
            Log.d(TAG, "--- REGISTERD UID::::::::: " + uid);
            quantity = fromCart.getIntExtra("quantity", 1);
            productName = fromCart.getStringExtra("cart_product_name");
            // total = fromCart.getDoubleExtra("total", 0.0);

            emailCart = preferences.getString("email", null);

        }else if (loginflagforguest){

        }else{

        }

    }
}
G Ganesh
  • 103
  • 7

3 Answers3

1

you don't need to use this Intent fromCart = getIntent();,because in constructor you already pass intent ,then just use intent object

dvId = intent.getStringExtra("Advid");
            price = intent.getDoubleExtra("price", 0.0);
            num = intent.getStringExtra("num");
            uid = intent.getStringExtra("uid");
            Log.d(TAG, "--- REGISTERD UID::::::::: " + uid);
            quantity = intent.getIntExtra("quantity", 1);
            productName = intent.getStringExtra("cart_product_name");
            // total = intent.getDoubleExtra("total", 0.0);
rachna
  • 124
  • 8
0

there is two ways to passing the data to the next activity you can either use intent or can use local broadcast receiver

if you want to fetch data from the previous activity then use

String a= getIntent().getStringExtra( "");// pass the name that you used in the previous activity
John Joe
  • 12,412
  • 16
  • 70
  • 135
rachna
  • 124
  • 8
  • 1
    This was posted as an answer, but it does not attempt to answer the question. It should possibly be an edit, a comment, another question, or deleted altogether. – John Joe Oct 17 '19 at 07:08
  • I want to fetch data to a class not to activity. Activity -> .java class. ->Activity. Data passing and fetching should be done by a normal java class. – G Ganesh Oct 17 '19 at 07:16
0

You have already pass parameter intent to addToCart function, so instead of advId = fromCart.getStringExtra("Advid");, you can use advId = intent.getStringExtra("Advid");

John Joe
  • 12,412
  • 16
  • 70
  • 135