-2

after pressing the "Send Data" button I want to send two numbers from MainActivity to SecondActivity. With this I have no problem, everything works fine. But then, I would like the addition, subtraction, multiplication and division operations on the received numbers to be performed in SecondActivity. And if I return to MainActivity the results have been returned. But I can't do it, what should I do?

This is my MainActivity:

public class MainActivity extends AppCompatActivity {

    EditText FirstNumber, SecondNumber;
    TextView Results, Addition, Subtraction, Multiplication, Division;
    Button SendData;

    float Add, Sub, Multi, Div;



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

        FirstNumber = findViewById(R.id.editText);
        SecondNumber = findViewById(R.id.editText2);
        Results = findViewById(R.id.textView);
        Addition = findViewById(R.id.textView2);
        Subtraction = findViewById(R.id.textView3);
        Multiplication = findViewById(R.id.textView4);
        Division = findViewById(R.id.textView5);
        SendData = findViewById(R.id.button);

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

                float Number1 = Float.parseFloat(FirstNumber.getText().toString()); 
                float Number2 = Float.parseFloat(SecondNumber.getText().toString()); 

                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                i.putExtra("Number1", Number1); 
                i.putExtra("Number2", Number2); 
                startActivity(i);

            }

        });


    }
}

And this is my SecondActivity:

public class SecondActivity extends AppCompatActivity {



    Float Number1; 
    Float Number2;

    Float Add;
    Float Sub;
    Float Multi;
    Float Div;

    TextView FirstNumb;
    TextView SecondNumb;

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

        FirstNumb = findViewById(R.id.textView7);  
        SecondNumb = findViewById(R.id.textView8);

        Number1 = getIntent().getFloatExtra("Number1", 0); 
        Number2 = getIntent().getFloatExtra("Number2", 0);


        FirstNumb.setText("First Number: " + String.valueOf(Number1));
        SecondNumb.setText("Second Number: " + String.valueOf(Number2));
    }
}
``;
MD Naseem Ashraf
  • 1,030
  • 2
  • 9
  • 22
Lukasso
  • 15
  • 1
  • You want to performed all `arithmetic operation` perform on numbers i.e add,mult etc? – Swati Mar 18 '19 at 05:33
  • 1
    Here you go: https://stackoverflow.com/a/54058428/8034839 – shizhen Mar 18 '19 at 05:44
  • Read this: https://developer.android.com/training/basics/intents/result You can also start another activity and receive a result back. To receive a result, call startActivityForResult() instead of startActivity(). Follow the steps in the linked Google documentation to add the feature of returning results to calling activity. – MD Naseem Ashraf Mar 18 '19 at 06:06

3 Answers3

0

In the second activity save your data using sharedPreferences after performing all the arithmetic operations

And in mainActivity's oncreate retrieve the data.

You can save data using

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("name", "Elena"); editor.putInt("idName", 12); editor.commit();

Retrieve data using

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("text", null); if (restoredText != null) { String name = prefs.getString("name", "No name defined");//"No name defined" is the default value. int idName = prefs.getInt("idName", 0); //0 is the default value. }
jp singh
  • 324
  • 2
  • 13
0

You can get back the data from SecondActivity to MainActivity as result for this notice this line startActivityForResult(i, REQUEST_CODE); in MainActivity's sendData click

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

                float Number1 = Float.parseFloat(FirstNumber.getText().toString()); 
                float Number2 = Float.parseFloat(SecondNumber.getText().toString()); 

                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                i.putExtra("Number1", Number1); 
                i.putExtra("Number2", Number2); 
                startActivityForResult(i, 2);

            }

        });

And override onActivityResult method in the same MainActivity like this

@Override  
       protected void onActivityResult(int requestCode, int resultCode, Intent data)  
       {  
                 super.onActivityResult(requestCode, resultCode, data);  
                  // check if the request code is same as what is passed  here it is 2  
                   if(requestCode==2)  
                         {  
                            String message=data.getStringExtra("result");   
                            textView1.setText(message);  
                         }  
     }  

And in SecondActivity you can send back data like this

sendResultback.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View arg0) {  
                    String operation = String.valueOf(Number1+Number2);
                    Intent intent=new Intent();  
                    intent.putExtra("MESSAGE",operation);  
                    setResult(2,intent);  
                    finish();//finishing activity  
                }  
            });
Abid Khan
  • 2,451
  • 4
  • 22
  • 45
-2

You are doing great.And you want to send data to first activity after performing all airthmetic operartions. You can use this way :-

Using Shared Preference

1)After performing all operations save your data on Shared Preference and fetch your data when you back on first Activity.

You can easily add/retrieve data using SharedPreference. Follows these are steps:-

  1. Define SharedPreference.Editor

    SharedPreferences.Editor editor;

  2. Put the values in SharedPreference Like as:--

    editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();

    editor.putInt("sum", value_of_sum);

    editor.putInt("difference", value_of_difference);

    editor.putInt("multiply", value_of_mul);

    editor.putInt("div", value_of_div);

    editor.apply();

  3. Retrieve data on Second Activity like:-

    SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);

    int sum = prefs.getInt("sum", null);

    int dif = prefs.getInt("difference", null);

    int mul = prefs.getInt("multiply", null);

    int div = prefs.getInt("div", null);

You can take help from this link for further on SharedPreferences. Hope this will help you.Thanks.....

Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30