0

I am trying to pass private String from MainActivity to another activity. Here is the code:

MainActivity

public class MainActivity extends AppCompatActivity {
private String data1;
private String data2;

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

    //Direct to data Input page and then to the next page
    TextView text = findViewById(R.id.data_text);
    text.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setContentView(R.layout.datalayout);
            EditText data = findViewById(R.id.Data1);
            MainActivity.this.data1 = data.getText().toString();
            data = findViewById(R.id.Data2);
            MainActivity.this.data2 = data.getText().toString();
            v = (ImageView) findViewById(R.id.continueNext);
            MainActivity.this.redirectToAnotherPage(v, MainActivity.this, AnotherActivity.class);

        }
    });

private void redirectToAnotherPage(View view, final Context baseActivity, final Class newActivity) {
    view.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(baseActivity, newActivity);
            //Pass data between MainActivity and the launching activity using bundle
            Bundle bundle = new Bundle();
            bundle.putString("Data1",data1);
            bundle.putString("Data2",data2);
            myIntent.putExtras(bundle);
            //End MainActivity before starting new activity
            MainActivity.this.finish();
            startActivity(myIntent);
        }
    });
}

AnotherActivity

public class AnotherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page);

    //Get data from MainActivity using Bundle
    Bundle viewData = getIntent().getExtras();
    String dataToInclude = viewData.getString("Data1");
    Toast toast = Toast.makeText(getApplicationContext(),dataToInclude,Toast.LENGTH_LONG);  
    toast.show();
}

When I pass data1 & data2, which are of String type, I do not see the toast message. What am I doing wrong here?

Kiran Cyrus Ken
  • 379
  • 1
  • 3
  • 17

3 Answers3

1

Your code is incomprehensible:
1) what are you doing? setContentView(R.layout.datalayout);

2) What is data in data.getText().toString();

If you see the toast with no text in it that means that dataToInclude is either null or empty.
So focus to check data1 and data2 variables.
Edit: When you click on text the listener is executed immediately:
the new layout is shown with empty EditTexts, their empty strings are stored in data1 and data2 and you open the new activity with empty strings in the bundle.

  • 1)datalayout is the page that shows edittext fields for the user to input the data 2)edittext returns charsequence, so I have to convert to string – Kiran Cyrus Ken Jul 29 '18 at 11:51
  • So you change the layout of the activity? –  Jul 29 '18 at 11:57
  • no . It is more like inserting a layout in between two layouts. Here is my purpose. I have 2 items and when clicked on one of them, it opens their appropriate app page. but in between the main page and the app page, I want to collect variables from the user and then on another click, proceed to the appropriate page. Thus, I have used two onclick listeners – Kiran Cyrus Ken Jul 29 '18 at 11:58
  • And you're using setContentView? Couldn't you use an invisible layout and make it visible? Anyway if you don't post full code you can't get help. What is data since it is not declared in the activity? –  Jul 29 '18 at 12:01
  • made some changes. would this help? – Kiran Cyrus Ken Jul 29 '18 at 12:05
  • @KiranCK what is going on here? For one hour you presented wrong code? –  Jul 29 '18 at 12:07
  • the top method has nothing to do with the error. as the bundle is carried out with the second activity, i just pasted oncreate method for reference. – Kiran Cyrus Ken Jul 29 '18 at 12:15
  • @KiranCK What are you talking about? You're passing the values of findViewById(R.id.Data1); and findViewById(R.id.Data2); with the bundle that could be empty and you think that this has nothing to do with your problem? –  Jul 29 '18 at 12:20
  • You just shed some light. I was just checking the second method for values. I guess I had to see the first method now. I am really sorry for not being careful enough about the first method.Thank you – Kiran Cyrus Ken Jul 29 '18 at 12:25
  • still what do you mean? –  Jul 29 '18 at 12:27
  • the bundle is empty when it is passed to another activity. but i am typing some values on edittext.. why does it not get saved to data1 or data2? – Kiran Cyrus Ken Jul 29 '18 at 12:29
  • k. got it. I have to use the edittext in the second method – Kiran Cyrus Ken Jul 29 '18 at 12:40
0

You are not calling toast.show(). Do like this -

Toast toast = Toast.makeText(getApplicationContext(),dataToInclude,Toast.LENGTH_LONG);
toast.show();

or

Toast.makeText(getApplicationContext(), dataToInclude, Toast.LENGTH_LONG).show();
Raj
  • 2,997
  • 2
  • 12
  • 30
  • I had that already. I should've been more clear, when I said I can not see the toast message. I can see the toast, but not the message inside it. – Kiran Cyrus Ken Jul 29 '18 at 10:55
  • I did that. I just missed to paste it here. That is not the problem. I can see the toast but it is just a blank toast. no value is shown in toast – Kiran Cyrus Ken Jul 29 '18 at 11:00
  • I've added the full code as you asked. toast is not my intention, i just used it to see if i get the values passed to toast – Kiran Cyrus Ken Jul 29 '18 at 11:37
  • MainActivity.this.data1 = data.getText().toString(); - What is data here and where you have initialized or created it ? @KiranCK – Raj Jul 29 '18 at 11:39
  • that is an edittext. I manually enter the values in the app and store it to the variable data1. – Kiran Cyrus Ken Jul 29 '18 at 11:42
  • Is it present in main activity layout or data layout ? Where have you initialized data ?? Try to log the value of data and see if something appears.. – Raj Jul 29 '18 at 11:44
  • Here is my purpose. I have 2 items and when clicked on one of them, it opens their appropriate app page. but in between the main page and the app page, I want to collect variables from the user and then on another click, proceed to the appropriate page. Thus, I have used two onclick listeners. I have in initialized data in the onCreate() method – Kiran Cyrus Ken Jul 29 '18 at 11:47
  • I made some changes. would this help? – Kiran Cyrus Ken Jul 29 '18 at 12:04
0

You may just use putExtra method on intent like following

myIntent.putExtra("key", value);

and in the other activity just get the data with intent as follwing

String value = getIntent().getStringExtra("key");