Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) The Model-View-Controller Pattern Adding a Custom Constructor

Intents

Hi, so I'm doing the extra credits and I have two questions :D

1: I want to pass an Object through intent, is that possible? I searched and found that I need to convert the object class into a parcelable class, but I was confused and couldn't do it.

2: my other question is that I have 3 activities, I'm passing a text from the first activity to the second using an intent, and I want to pass another string from the second activity to the third using putExtra, the first putExtra is working fine, but the second one just doesn't receive the sent data. Is it getting confused with the previous intent? is their any solution to this problem?

Thanks :D

2 Answers

Hi Ihssan,

Concerning your first question, a more simple solution than using Parcelable is to implement Serializable on your Class, this way you could use [this] http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, java.io.Serializable)

intent.putExtra("myObject", myObject);
// And to retrieve the object you could
getSerializableExtra("myObject");

For your second question, can you share your code probably here or on Github?

Thanks :)

Thanks for responding :) I tried the serializablation, but it's not working probably because of the same problem I asked about in the second question.

Here I'm sending the object from the first activity:

         mFinishButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent sendInfo = new Intent(ShowResult.this, UserInfo.class);
                sendInfo.putExtra("name", mUser);
                startActivity(sendInfo);
            }
        });

Here i'm getting the intent from the other activity I tried the same thing with sending texts through the intent and it didn't work too, it is not receiving any texts the string becomes empty.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        mFinishButton = (Button) findViewById(R.id.finishButton);
        mUserInfoBoard = (TextView) findViewById(R.id.userInfoBoard);

        Intent sendInfo = getIntent();
        mUser = (User) sendInfo.getSerializableExtra("name");
        mUserInfoBoard.setText("User name: " + mUser.getNameText());


        mFinishButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

Fantastic! you just need to cast to Serializable when you putExtra, and make sure your User class implements Serializable.

 mFinishButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent sendInfo = new Intent(ShowResult.this, UserInfo.class);
                sendInfo.putExtra("name", (Serializable) mUser);
                startActivity(sendInfo);
            }
        });

Hope this helps :)

Thanks a lot man, it's all working now and I was able to move the whole object :D feels great :) Thanks!