-2

So, here is the thing- I have a mainactivity class and a Splash class. The mainactivity contains two TextViews and one button. Now, i have my splash class which has two editTexts and one Ok Button. My goal is that when i run the android program, the main activity loads up. Then i click on the button and the Splash activity loads up. In the splash activity, i enter two numbers in the EditText boxes and the numbers will be captured in the Intent Object and returned in the appropriate TextView lables in the Main Activity. Now, when i try to capture one value and return that value in the original activity(Main Activity), my code works fine but when i try with two values and use the same concept, the second number that i enter in the Splash activity overwrites the first activity and i see the second number repeated twice. How do i fix this? How can i capture more than one values in an intent and return them in their appropriate labels in the Main Activity class. Here is the code:-

MainActivity.java

package com.example.user.androidpractise4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button b;
    int code = 1;
    String a,c;
    int num;
    int num1;
    TextView t,v;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        b = (Button)findViewById(R.id.Btn1);
        t = (TextView)findViewById(R.id.textView2);
        v = (TextView)findViewById(R.id.TextV);

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

                startActivityForResult(new Intent("com.example.user.androidpractise4.SPLASH"), code);


            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == code) {
            if (resultCode == RESULT_OK) {

                // Toast.makeText(this, data.getData().toString(),
                //Toast.LENGTH_SHORT).show();
                a = data.getData().toString();

                num = Integer.parseInt(a);
                t.setText("" + num);

//This is the part where i tried to improvise but it doesnt work

                c = dat.getData().toString();
                num1 = Integer.parseInt(c);
                t.setText("" + num1);
                int total = num1 + num;

            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Splash.java:-

package com.example.user.androidpractise4;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by user on 11-08-2015.
 */

public class Splash extends Activity {
    Button b1;
    EditText display,disp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        b1 = (Button)findViewById(R.id.button);
        display = (EditText)findViewById(R.id.editText);
        disp =(EditText)findViewById(R.id.editText2);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent data = new Intent();
                data.setData(Uri.parse(display.getText().toString()));
                setResult(RESULT_OK, data);

//I have added the below part to see if i can capture more than one value from //Splash activity using the Intent and return it to the MainActivity.

                Intent dat = new Intent();
                dat.setData(Uri.parse(disp.getText().toString()));
                setResult(RESULT_OK, dat);

                finish();



            }
        });
    }
}
Missy Zewdie
  • 231
  • 2
  • 9
  • 17

2 Answers2

4

You should put anything you need in the result intent and then get them from extras.

just do it like this :

in Splash:

b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent data = new Intent();
            data.putExtra("n1", display.getText().toString());
            dat.putExtra("n2", disp.getText().toString());
            setResult(RESULT_OK, dat);

            finish();

        }
    });

and in MainActivity:

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == code) {
        if (resultCode == RESULT_OK) {

            // Toast.makeText(this, data.getData().toString(),
            //Toast.LENGTH_SHORT).show();
            a = data.getStringExtra("n1");

            num = Integer.parseInt(a);
            t.setText("" + num);
            c = data.StringExtra("n2");
            num1 = Integer.parseInt(c);
            t.setText("" + num1);
            int total = num1 + num;

        }
    }
}
Sina KH
  • 563
  • 4
  • 16
0

You can access you second Activity public fields to update a value.

Cyprien Aubry
  • 641
  • 8
  • 19