Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build an Interactive Story App Intents and Multiple Activities Sending Data to a New Activity

Why couldn't you set the variable as private and use it throughout the project?

They're asking to have a parameter in the "StartStory" method but instead of passing a parameter why couldn't you just define what name is in the beginning like so?

public class MainActivity extends AppCompatActivity {

    private EditText mEditText;
    private Button mStartButton;
    private String mName;

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

        mEditText = (EditText) findViewById(R.id.nameEditText);
        mStartButton = (Button) findViewById(R.id.startStory);

        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mName = mEditText.getText().toString();

                if (mName.matches("")) {
                    Toast.makeText(MainActivity.this, "Please enter a valid User name", Toast.LENGTH_LONG);
                    return;
                } else {
                    startStory(mName);
                }
            }
        });
    }

    private void startStory(String mName) {
        Intent intent = new Intent(this, StoryActivity.class);
        intent.putExtra("UserName", mName);
        startActivity(intent);
    } 

1 Answer

Ranvir Sahota
Ranvir Sahota
9,844 Points

Though this question may be old I will answer it for anyone else that may get confused. The question was answered in the video but Ben did simply not explain in-depth. The keyword was "coupling" which is defined as a principle in OOP as a way of separation of concerns. The aim to prevent objects being intrinsically tied together,. this helps keep code clean and reusable as stated in the video.

Look at this link: https://gamedevelopment.tutsplus.com/tutorials/quick-tip-the-oop-principle-of-coupling--gamedev-1935 Which is how I came to understand this concept.