Skip to content Skip to sidebar Skip to footer

The Intent Transform My List Into An Empty List When It Is Transported Into The Main Activity

I have a problem, my List named tag is empty after appending item in it with the button in my second activity. The intent has a bad effect in my tag, and it is finally null in the

Solution 1:

You are not putting tag list in intent on button2 click.

Replace your second activity code with this:

publicclassSecondActivityextendsAppCompatActivity {
//@RequiresApi(api = Build.VERSION_CODES.N)
String name;
ArrayListtag=newArrayList();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_main);


    EditText edit;
    Button button;

    Intentintent=newIntent(getBaseContext(), SecondActivity.class);

    edit = (EditText) findViewById(R.id.edit);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            name = edit.getText().toString();
            tag.add(name);
            Toast.makeText(getApplicationContext(), tag.toString(), 
            Toast.LENGTH_SHORT).show();
        }
    });
    intent.putStringArrayListExtra("tag", tag);
    Button button2;
    button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            Toast.makeText(getApplicationContext(), tag.toString(), 
            Toast.LENGTH_SHORT).show();
            startActivity(intent);
        }
    });
    Button button3;
    button3 = (Button) findViewById(R.id.button3);
    button3.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            IntentretourgameActivity=newIntent(SecondActivity.this, 
            MainActivity.class);
            retourgameActivity.putStringArrayListExtra("tag", tag);
            startActivity(retourgameActivity);
        }
    });
}
}

Post a Comment for "The Intent Transform My List Into An Empty List When It Is Transported Into The Main Activity"