-2

I'm looking for an answer for days now but still can't find it so let's hope someone will help me on this one.

I have X activities, and i would like to pass different variables from these activities to only ONE activity. When i use intent put extra it works well from 1 activity to the final activity, but when i use it from 2 activities to the final activity, the app is crashing down. Can someone tells me how to do it ?

Here's an exemple code of an X activity :

public class AkyluxClass extends Activity {

//Initialisation
double prix;
EditText longueur;
EditText largeur;
EditText quantite;
TextView total;
Button calcul, facturer, valider;
float coefficient;
public final static String COEF = "coefficient";
String test;

double num1, num2, num3, resultat;

@Override


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.akylux);

    //Association layout / code
    prix = 10.90;
    longueur = (EditText) findViewById(R.id.longueur_akylux);
    largeur = (EditText) findViewById(R.id.largeur_akylux);
    quantite = (EditText) findViewById(R.id.quantite_akylux);
    total = (TextView) findViewById(R.id.total_akylux);
    calcul = (Button) findViewById(R.id.button_calcul_akylux);
    facturer = (Button) findViewById(R.id.button_facturer);
    valider = (Button) findViewById(R.id.valider_akylux);
    coefficient = getPreferences(MODE_PRIVATE).getFloat(COEF, 1);


    //Listener du bouton calcul
    calcul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            num1 = Double.parseDouble(longueur.getText().toString());
            num2 = Double.parseDouble(largeur.getText().toString());
            num3 = Double.parseDouble(quantite.getText().toString());
            resultat = ((num1 * num2)/10000) * num3 * prix * coefficient;
            total.setText(Double.toString(resultat));

            if (num1 < 100) {
                num1 = 100;
            }
            if (num2 < 100) {
                num2 = 100;
            }

            if (num3 < 1) {
                num3 = 1;
            }


        }
    });


    //Listener du bouton valider
    valider.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String valStr = String.valueOf(resultat);
            Intent intent = new Intent(AkyluxClass.this, DevisClass.class);
            intent.putExtra("akylux", valStr);
            startActivity(intent);
             }
        });
    }
  }

And here's the code of the final activity :

public class DevisClass extends Activity {

//Initialisation des variables
TextView valeur_akylux, valeur_blanc, valeur_carton, valeur_dibond, valeur_gris, valeur_pvc, total_Devis;
Button facturer;
double total_devis_double, resultat;
String total;


static AkyluxClass AkyluxClass;
static PvcClass PvcClass;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.devis);

    //Association layout / code
    facturer = (Button) findViewById(R.id.button_facturer);
    valeur_akylux = (TextView) findViewById(R.id.valeur_akylux_devis);
    valeur_blanc = (TextView) findViewById(R.id.valeur_blanc_devis);
    valeur_carton = (TextView) findViewById(R.id.valeur_carton_devis);
    valeur_dibond = (TextView) findViewById(R.id.valeur_dibond_devis);
    valeur_gris = (TextView) findViewById(R.id.valeur_gris_devis);
    valeur_pvc = (TextView) findViewById(R.id.valeur_pvc_devis);
    total_Devis = (TextView) findViewById(R.id.total_Devis);


    //On récupère la variable passée via l'intent de la première classe
    String akylux_extra = (String) getIntent().getSerializableExtra("akylux");

    //on l'attribue à un double pour avoir une valeur calculable
    double valaky = Double.parseDouble(akylux_extra.trim());

    //On l'affiche sur le label du layout
    valeur_akylux.setText(Double.toString(valaky));

    //Test : on l'attribue à un total
    total_devis_double = Double.parseDouble(valeur_akylux.getText().toString());

    //resultat = valaky + blanc + carton + dibond + gris + pvc;
      resultat = valaky;

    //On transforme le total en String
      total = String.valueOf(total_devis_double);

    //On affiche le String total
    total_Devis.setText(total);

    //Listener du bouton facturer
    facturer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"rayan@eprint.fr"});
            i.putExtra(Intent.EXTRA_SUBJECT, "Votre facture deviseur ePrint");
            i.putExtra(Intent.EXTRA_TEXT   , new String[]{"Voici le détail de votre facture : " , total, "total", "€"});
          //i.putExtra(Intent.EXTRA_TEXT   , new String[]{total});
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Toast.makeText(DevisClass.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            }
        }
    });
}
}

Thank you for helping

Oxy180
  • 11
  • 3

5 Answers5

1

For the String passing you can change the receiving class intent to read String Extra and also add null bundle check. The error log would help to pin-point the issue. Maybe the data is not saved properly somehow...

Bundle bdlExtra = getIntent().getExtras();
if(bdlExtra != null) {
String akylux_extra =bdlExtra.getString("akylux");
}
Alex T
  • 163
  • 1
  • 9
1

Suppose you have a Home Activity A and many other activities B,C,D from where you want to send tha data to activity A.

In activity A, declare a String Tag :

public static final String    TAG_ACTIVITY_FROM = "WhichActivity";

In other activities, send :

//you can send as many data as you want in putExtra

     Intent intent = new Intent(D.this, A.class);
        intent.putExtra(A.TAG_ACTIVITY_FROM, "From_D");
    intent.putExtra(A.VALUE, 4);
        startActivity(intent);

In A, get it back with :

 if (getIntent().getExtras() != null) {
            String activityFrom =(String)getIntent().getStringExtra(TAG_ACTIVITY_FROM);
int value = getIntent().getIntExtra(VALUE, -1);
        }
Vishesh
  • 308
  • 1
  • 9
  • when your getting the data use getIntent().getDoubleExtra("akylux", -1); – Vishesh May 11 '17 at 09:09
  • bcz you are passing the double value – Vishesh May 11 '17 at 09:10
  • Hi, it looks like it can work, but what should i put instead of -1 ? when i run the app, it only makes appear "-1" – Oxy180 May 11 '17 at 09:41
  • public double getDoubleExtra (String name, double defaultValue) So The second parameter will be the default value that you get if your String variable doesnt contain any value. – Vishesh May 11 '17 at 09:48
  • well, it either write -1 as a result or crash down. Don't know what to do – Oxy180 May 11 '17 at 10:05
  • as u said it prints -1 , so this means that the variable you are passing doesnot contain any value . so it is returning you the default value . try to debug the code for the values and also look for the variables that you are sending is of same type as you are recieving – Vishesh May 11 '17 at 10:12
  • Ok you were right, the type was the problem, it almost work. The last issue is that when i pass a variable from B to A, the value is in A, but when i go to C and pass a variable from C to A, only the C value is in A, the B's one has disappeared and is now "0", what should i do ? – Oxy180 May 11 '17 at 10:33
  • i dont get what u are trying to say. From what i understand you should store the result of B ,either in shared preference or in some other Plain class ,so that you can refer to that class later – Vishesh May 11 '17 at 10:38
  • ok so activity A contain a list. When you enter a value in B and press enter the B value goes into the A list. When i go to the activity C and do the same, so the C value goes into the A list, but the B value has disappeared from the A list, but i want them both to stay – Oxy180 May 11 '17 at 11:28
  • You are going wrong somewhere because once you passed the data from B to A ,then you store that data in some variable which is in scope of A.So that variable has nothing to do with the scope of B's activity. Anyhow, make another class for storing the passed values .when you get the value in A ,store that value in the new class and now your list items will point to the variables of new class and the value is not getting 0 – Vishesh May 11 '17 at 11:40
  • i'll try this, thanks alot, i'll feedback when it's done – Oxy180 May 11 '17 at 11:58
  • give this answer a up arrow , so it will be useful for others as well. Happy coding :) – Vishesh May 11 '17 at 11:59
  • It definitely works. Thanks alot man, you saved me !!!!!!! i put it as accepted answer – Oxy180 May 11 '17 at 12:30
0

If it is working on one activity it shouldn't be a problem for the next Activity, we won't know for certain unless you show your Log specifically errors displayed, apart from put extra in Intents, why don't you use:

Would be an excellent way to share primitive data types for your whole app usage.

Cheers.

alkathirikhalid
  • 887
  • 12
  • 18
  • Hi, first of all, thank you for the fast answer, but i've already tried to use shared preference and can't figure out to make it work, looks like nothing happens when i use it. Would it be a better solution ? thank you – Oxy180 May 11 '17 at 08:09
  • Apparently there's a nullpointerexception on the final activity according to the log – Oxy180 May 11 '17 at 08:55
  • That's great, so now we know that the final Activity isn't receiving anything, add a checking for null, you can have a static reference so the object/value reference stays in memory pay attention to: public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; Tutorial: https://developer.android.com/training/basics/firstapp/building-ui.html if my answer/reply is helpfu/useful give it a raise. Cheers. – alkathirikhalid May 12 '17 at 01:57
0

I think you can use a class as a handler or controller using static variables or functions:

    class Handler{
         //value
         public static Double result = 0.0;
         //set of values
         public static List<Double> resultArr = new ArrayList<>();
        }

Then you can pass values from any other class and test the values in the final activity, especially if you are in multi threading Data process . Hope Help :)

0

There are many ways to do. In your first activity. MyFirstActivity.java

 String password="password";
 String mypassword="sfsdf234234";
 Intent myintent = new Intent(MyFirstActivity.this,MySecondActivity.class); 
 myintent,putExtra("Email","rohit@gmail.com"); 
 myintent.putExtra(password,mypassword); 
 startActivity(myintent);

Now from your Second activity. MySecondActivity.java you can get the data in this way.

Intent myintent = getIntent();
String myPassword =myintent.getExtra("password"); 
String myEmail =myintent.getEXtra("email");

Hope this helps you. You can also check this post. Same concept.

Community
  • 1
  • 1
10zin
  • 79
  • 4