1

Hello I want to call an object(username, name, posts) from Mainactivity.java to Main2Activity.java first layout: The user enter write here name, username and posts and click on the button
second layout: the information the user has given is displayed on the page I want to save the name, username and posts to used in Second layout

https://ibb.co/BfsKMgf

public class MainActivity extends AppCompatActivity {

    public String username;
    public String name;
    public String posts;
    EditText usernameinput;
    EditText nameinput;
    EditText postsinput;
    Button confirme;

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

        setContentView(R.layout.activity_main);
        usernameinput = (EditText)findViewById(R.id.username);
        nameinput = (EditText)findViewById(R.id.name);
        postsinput = (EditText)findViewById(R.id.posts);
        confirme = (Button)findViewById(R.id.confirme);
        username = usernameinput.getText().toString();

        confirme.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                name = nameinput.getText().toString();
                posts = postsinput.getText().toString();

                Intent otheractivity = new Intent(getApplicationContext(),Main2Activity.class);
                startActivity(otheractivity);
                finish();

                showToast(name);
                showToast(username);
                showToast(posts);


            }
        });



    }


    private void showToast(String text){
        Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
    }

}
Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • Possible duplicate of [How to pass an object from one activity to another on Android](https://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android) – Thecave3 Jun 24 '19 at 14:23

2 Answers2

1

using intent to pass data from one activity to another activity or if you can use fragment then use bundle. follow these tutorials - https://developer.android.com/training/basics/firstapp/starting-activity#java

you have to change the code.

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putStringExtra("key1","editetextvalue")
intent.putStringExtra("key2","editetextvalue2")
startActivity(intent);

and get value in a second activity.
Intent intent = getIntent();
String key = intent.getStringExtra("key1")

please remember if you pass these key exact same get key other not get
axar
  • 539
  • 2
  • 17
1

Many ways to pass data from one activity to second activity for example using Intent to pass data from one activity to another,another way use sharedpreference to store data and get it anywhere, third store data in static variable

using intent to pass data

MainActivity.java

  nameEt = (EditText) findViewById(R.id.name_et);
    mobileEt = (EditText) findViewById(R.id.mobile_et);
    submitBtn = (Button) findViewById(R.id.submit_btn);
    submitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name=nameEt.getText().toString();
            mobile_number=mobileEt.getText().toString();
            Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
            intent.putExtra("name",name);
            intent.putExtra("mobile",mobile_number);
            startActivity(intent);
        }
    });

SecondActivity

 nameTv = (TextView) findViewById(R.id.name_tv);
    mobileTv = (TextView) findViewById(R.id.mobile_tv);

    String name,mobile_number;
    //get Intent data
    name=getIntent().getStringExtra("name");
    mobile_number=getIntent().getStringExtra("mobile");
    nameTv.setText(name);
    mobileTv.setText(mobile_number);

using static variable

MainActivity.java public static String name; public static String mobile_number;

 nameEt = (EditText) findViewById(R.id.name_et);
    mobileEt = (EditText) findViewById(R.id.mobile_et);
    submitBtn = (Button) findViewById(R.id.submit_btn);
    submitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name=nameEt.getText().toString();
            mobile_number=mobileEt.getText().toString();
            Intent intent=new Intent(FirstActivity.this,SecondActivity.class);

            startActivity(intent);
        }
    });

SecondActivity.java

nameTv = (TextView) findViewById(R.id.name_tv);
    mobileTv = (TextView) findViewById(R.id.mobile_tv);
 nameTv.setText(MainActivity.name);
 mobileTv .setText(MainActivity.mobile_number);
Android Geek
  • 8,956
  • 2
  • 21
  • 35