Home > database >  Issues sharing a string between activities
Issues sharing a string between activities

Time:10-07

I'm new at Android Programming and I'm having issues while trying to pass a String from an activity and an other one. I've already added the needed code in manifest file. I would like to take "myName" from user input in MainActivity and pass it to activity_matching, but in this second file the variable is always empty. I can't understand what I'm doing wrong, so I'm here.

package com.ium.example.packageName;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity{
    String myName;
    EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        edit = findViewById(R.id.nickName);
        myName = edit.getText().toString();
        Button loginButton = findViewById(R.id.button);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, activity_matching.class);
                i.putExtra("myName", myName);
                startActivity(i);
            }
        });
    }
}

The code above is related to "MainActivity", the code below to "activity_matching"

    package com.ium.example.packageName;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;

import java.util.Random;

public class activity_matching extends AppCompatActivity {
    float x1,x2,y1,y2;
    int s;
    String[] names = new String[17];
    public String myName;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_matching);
    }

    public boolean onTouchEvent(MotionEvent touchEvent){
        switch(touchEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = touchEvent.getX();
                y1 = touchEvent.getY();
                break;

            case MotionEvent.ACTION_UP:
                x2 = touchEvent.getX();
                y2 = touchEvent.getY();
                Intent i = new Intent(activity_matching.this, SwipeLeft.class);
                i.putExtra("num", s);
                i.putExtra("arr", names);
                startActivity(i);
                break;
        }
        return false;
    }

    public void onBtnClick(View v) {
        TextView txtHello = findViewById(R.id.txtMessage);
        Intent b = getIntent();
        myName = b.getStringExtra("myName");
        String swipeLeft = "Swipe (a destra o sinistra) per l'argomento di discussione";
        Snackbar doSwipe = Snackbar.make(v, swipeLeft, 6000);
        Random rand = new Random(System.currentTimeMillis());
        names = new String[]{"Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8",
                             "Name9", "Name10", "Name11", "Name12", "Name13", "Name14", "Name15",
                             "Name16", "Name17"};
        re:
        {
            s = rand.nextInt(names.length);
            if (myName.equals(names[s]))
                break re;
            txtHello.setText(myName   " vai a parlare con "   names[s]   "!");
            doSwipe.show();
        }
    }
}

Thanks to everyone who will answer :3 Have a nice day ^-^

CodePudding user response:

It may help to try to get your intent's extra data inside of the onCreate method. I'm not sure if this will help but I was having trouble replicating your problem on my end.

CodePudding user response:

in MainActivity, you assign myName before you click loginButton. Your EditText was still empty at that time. move:

myName = edit.getText().toString();

to onClick above this line

i.putExtra("myName", myName);
  • Related