0

I have 2 EditTexts and I want to copy the String of these EditTexts and use them in a method in another activity.

I was making an app which types some details in two EditTexts and have a button which copies the String of these two EditTexts and paste or use it in a RecyclerView in another Activity.

I tried the Intent and Bundle medthods but could not solve it and actually it was hard to arrange the structure of codes.

This is the activity I want to pass from:

btn_save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etTitle.length() != 0 || etDes.length() != 0){
                addData();
            }else {
                Toast.makeText(DataInput.this, "Please Add Data !", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

private void addData() {
    String titled = etTitle.getText().toString();
    String desed = etDes.getText().toString();
    Intent inte = new Intent();
    Bundle bund = new Bundle();
    bund.putString("title", titled);
    bund.putString("des", desed);
    inte.putExtras(bund);
    startActivity(inte);
}

This is the activity I want to receive with:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), DataInput.class);
            startActivity(intent);
        }
    });

    recyclerView = findViewById(R.id.rv);

    dAdapter = new DataAdapter(dataList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(dAdapter);
}

public void sendData() {
    Bundle bundle = getIntent().getExtras();
    String addedTitle = bundle.getString("title");
    String addedDes = bundle.getString("dec");
    Data data = new Data(addedTitle, addedDes);
    dataList.add(data);
    dAdapter.notifyDataSetChanged();
}

All I want is to pass the intent and bundle from addData method in the first Activity to the sendData method in the second Activity, so I can use the Strings to pass them in Data constructor.

  • Can you add some code to see what are your first tries? I think that to pass data between activities the best approach is using intent extras, but first show us your attempts. – Josep Oriol Soler Aug 07 '18 at 14:45
  • Use a [Bundle](https://developer.android.com/reference/android/os/Bundle). You can store key value pairs as parameters to pass to the other Activity. – Michael Krause Aug 07 '18 at 14:48
  • I add some code – AbdallahBedo Aug 07 '18 at 15:03
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Shubham AgaRwal Aug 07 '18 at 15:09
  • `Intent inte = new Intent();` this is wrong. you have to specify the activity class you supposed to launch. – Krishna Sharma Aug 07 '18 at 15:26

3 Answers3

0

To retrieve the text from an EditText you can use editText.getText().toString()

ArcDexx
  • 453
  • 5
  • 15
0

Use bundle or intent.

// From activity1 to activity2
    Intent intent = new Intent(Activity1.this, Activity2.class);
    Bundle bundle = new Bundle();
    bundle.putString(<key>, <value>);
    intent.putExtras(bundle);
    startActivity(intent);

    // in activity 2 onCreate
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        // get the value from bundle based on key
    }

Here is the short example pass data from activity to activity

I would recommend you to change your implementation in following way so that we can avoid key mismatch issue accidently.

    @Override
    public void onClick(View v) {
        if (etTitle.length() != 0 || etDes.length() != 0){
            String title = etTitle.getText().toString();
            String description = etDes.getText().toString();
            Activity2.launch(this,title,description);
        } else {
            Toast.makeText(DataInput.this, "Please Add Data !", Toast.LENGTH_SHORT).show();
        }
    }

In calling activity you may create helper method say launch like below.

    public static final String KEY_TITLE = "title";
    public static final String KEY_DESCRIPTION = "description";

    public static void launch(Context context, String title, String description) {
        Intent intent = new Intent(context, Activity2.class);
        Bundle data = new Bundle();
        data.putString(KEY_TITLE, title);
        data.putString(KEY_DESCRIPTION, description);
        intent.putExtras(data);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            String title = bundle.getString(KEY_TITLE);
            String description = bundle.getString(KEY_DESCRIPTION);
        }
    }
Krishna Sharma
  • 2,828
  • 1
  • 12
  • 23
0

To retrieve the text from an EditText use

String value = editText.getText().toString();

And then pass the key value pair either through intent or bundle

Intent in = new Intent(Activity1.this, Activity2.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("key", value);
startActivity(in);

to receive put this in new activity

String string_name = getIntent().getExtras().getString("key");

Update: There is key mismatch, you are sending key as "des" and receiving as "dec"

pd9
  • 124
  • 2
  • 11