Skip to content
Advertisement

Java Android String Intent displaying null when moving to previous screen

So I have a screen where a user enters their name, on button click they are redirected to a menu where it displays their name.

In the menu screen I have a button that takes me to another screen (About Me), on that screen I have a button with an Intent to go back to the Menu Activity.

The issue is that when I click the button with the Intent to go back, the name displays as null instead of what the User entered.

This does not happen when I use the actual android navigation buttons to go to the previous screen, only the Button i created to go back to the Menu Activity.


Launcher Activity where user enters their name

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

        Button launcherNextBtn = (Button) findViewById(R.id.launcherNextBtn);
        EditText launcherVisitorNameEditText = (EditText) findViewById(R.id.launcherVisitorNameEditText);
        TextView launcherVisitorNameErrorTextView = (TextView) findViewById(R.id.launcherVisitorNameErrorTextView);

        launcherVisitorNameErrorTextView.setText("");

        launcherNextBtn.setOnClickListener((View v) -> {

            String visitorName = launcherVisitorNameEditText.getText().toString();

            if (visitorName.equals("")) {
                launcherVisitorNameErrorTextView.setText("Please enter a value");
            } else {
                launcherVisitorNameErrorTextView.setText("");
                Intent goToMenuActivity = new Intent(LauncherScreen.this, MenuScreen.class);
                goToMenuActivity.putExtra("visitorName", visitorName);
                startActivity(goToMenuActivity);
            }
        });


Menu activity where it displays the users name

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

        Button menuHomeBtn = (Button) findViewById(R.id.menuHomeBtn);
        Button menuAboutMeBtn = (Button) findViewById(R.id.menuAboutMeBtn);
        TextView menuNameTextView = (TextView) findViewById(R.id.menuNameTextView);

        String visitorName = getIntent().getStringExtra("visitorName");
        menuNameTextView.setText("Dear " + visitorName);

        menuHomeBtn.setOnClickListener((View v) -> {
            Intent goToLauncherActivity = new Intent(MenuScreen.this, LauncherScreen.class);
            goToLauncherActivity.putExtra("visitorName", visitorName);
            startActivity(goToLauncherActivity);
        });
        
        menuAboutMeBtn.setOnClickListener((View v) -> {
            Intent goToAboutMeActivity = new Intent(MenuScreen.this, AboutMeScreen.class);
            startActivity(goToAboutMeActivity);
        });
    }


About me activity, when I click the button to back to Menu, i get “Dear null”

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

        Button aboutMeBackBtn = (Button) findViewById(R.id.aboutMeBackBtn);

        String visitorName = getIntent().getStringExtra("visitorName");


        aboutMeBackBtn.setOnClickListener((View v) -> {
            Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
            startActivity(goToMenuActivity);
        });
    }

Advertisement

Answer

Your problem is in About me activity screen

aboutMeBackBtn.setOnClickListener((View v) -> {
        Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
        startActivity(goToMenuActivity);
    });

This time you are not passing an extra field, so you are getting null

Answer 1

aboutMeBackBtn.setOnClickListener((View v) -> {
        onBackPressed();
    });

Answer 2

aboutMeBackBtn.setOnClickListener((View v) -> {
        Intent goToMenuActivity = new Intent(getApplicationContext(), MenuScreen.class);
        goToMenuActivity.putExtra("visitorName", visitorName)
        startActivity(goToMenuActivity);
    });
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement