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


Dhruv Gupta
Dhruv Gupta
4,320 Points

How to transfer data to a class which does not have the On.Create command

So I created an EditText box in which I get information that is saved to a variable. It is then transferred to another class via Intent. But I need that information in a different class which I can't seem to get Intent on due to a lack of On.Create in it. That class simply houses an array and creates a method in which the array is sequentially selected,

I've been looking online for hours now, but I haven't been able to find anything. Code below


super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNameField = (EditText) findViewById(R.id.nameeditText); mStartButton = (Button) findViewById(R.id.startbutton);

    mStartButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String name = mNameField.getText().toString();



public void StartStory(String name) {
    Intent intent = new Intent(this, StoryActivity.class);
    intent.putExtra("name", name);


public class StoryActivity extends Activity {

private Button mStoryButton;
private Story mStory = new Story();
private StoryPics mPic = new StoryPics();

protected void onCreate(Bundle savedInstanceState) {


    mStoryButton = (Button) findViewById(R.id.storybutton);
    final ImageView Storyp = (ImageView) findViewById(R.id.Story);
    final TextView StoryText = (TextView) findViewById(R.id.textView);
    Intent intent = getIntent();
    final String name = intent.getStringExtra("name");

    mStoryButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            int pict = mPic.getpic();
            String Storyt = mStory.getStory();

            Toast.makeText(StoryActivity.this, name, Toast.LENGTH_LONG).show();
            if (Storyt == ("You have arrived on Earth. You have come to a deserted woodland. Before you press the button you must ask yourself. If I were born to them would I have turned out any differently? Do the one percent give me anything? Do they deserve to be fuel?")) {
                Toast.makeText(StoryActivity.this, "To Be Continued", Toast.LENGTH_LONG).show();


public class Story extends StoryActivity{

public String[] mStory = {

        "The one percent had made the fateful mistake that led to the downfall of Rome. They made material goods the source of personal fulfillment. In their mad quest for resources they sent spaceships to Mars to mine the planet for precious resources.",
        "They had not expected that an advanced subterranian society lived beneath the surface of Mars. The Martians having felt the greed the 1 percent resolved to invade the planet and turn the bodies of the one percenters into fuel",
        "Before their invasion they have called you as a dedicated hater of the 1 percent to tell them weaknesses in the one percent. You tell them that human beings have a general angst due to existence. They cannot wrap their heads around it.",
         "You are hereby chosen to go to the spaceships and do what you must to do destroy the one percent. You go to the ship and record 'Everyone will eventually die. The difference is as you lay dying you will look back on a life where you let greed overcome empathy",
        "The aliens were startled that such a statement could possibly be novel to the one percent, but after further observations concluded that you were correct. The cute ragmuffins bid you farewell with their spaceship that could transmit the statement directly to every one percenter",
        "You have arrived on Earth. You have come to a deserted woodland. Before you press the button you must ask yourself. If I were born to them would I have turned out any differently? Do the one percent give me anything? Do they deserve to be fuel?",
int Storyc = 0;
public String getStory() {
    String Storyt = "";

    Storyt = mStory[Storyc];
    if (Storyc < mStory.length) {
        Storyc = Storyc+1;

    return Storyt;


1 Answer

Ryan Ruscett
Ryan Ruscett
23,309 Points


Intent is cool to use when passing information from one activity to another. But if you want to pass that same data into a different class. I would use an interface.

Create an interface in the class you are getting the data in. Have it have one method that takes a variable of some sort. Then, when you implement the interface in the destination class. You have to run that method, that method retrieves a variable. Pass it the data you want it to have and then in the method body, set it to some member variable or whatever you want. Then that class will have access to that data.

Not sure if this helps but try looking down creating an interface route. Should help.