Rubit Rubit - 4 months ago 7x
Android Question

What is the function of the "loginName = "Friend"" below?

The loginName here is input for username. I assume that the "loginName = "Friend" will display "Friend" as default username if there is no input, but it's not right. So what is the meaning of that code and if I want to display "Friend" as default, How can I do?

package com.example.rubit.interactivestory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.example.rubit.interactivestory.R;

public class Story extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {

Intent intent = getIntent();
String loginName = intent.getStringExtra(getString(R.string.login_name));

if (loginName == null) {
loginName = "Friend";


It is the default for when the input is null. But you probably have the issue that it's not null but empty (""). You should also check for:

if (loginName == null || TextUtils.isEmpty(loginName)) {
        loginName = "Friend";