0

OK, first off, bear with me as I am relatively new to Android Development.

So here is the situation. I have a parent (main) activity that records data. Lets say, for simplicity sake that it contains an edit box. Now the child (sub) activity contains a single text view that is currently blank. By default, when the app starts the main activity is started. Now when the user enters his name in the edit box and clicks a button I want that name to be appended to the text view on the second activity. I can then switch to the second activity and see that change. I then hit the back button and enter another name. When i switch to the second activity again i must see that change and so on. Obviously, the scenario does not only involve edit boxes and buttons but I think the idea is the same.

So in essence what I want to know is:

  1. How do i switch between activities and keep the data on each. I would have to create both activities at run time?
  2. How do I pass data between these activities?
  3. When i press the back button when the second activity is on focus how do i stop that activity from completely closing?

Thanks for all your help!

rameezk
  • 353
  • 2
  • 5
  • 15
  • Passing data between activities: http://stackoverflow.com/questions/2965109/passing-data-between-activities-in-android – Yaqub Ahmad Dec 07 '11 at 12:04

2 Answers2

0

You do this by using Intents. These basically start the other activity, and have the added bonus of being able to add extra information to them, which can be retrieved in the other activity.

In your main activity, when you click your button, add the following:

EditText edittext = (EditText) findViewById(R.id.editTextID);
Intent startSub = new Intent(this, SubActivity.class);
String input = edittext.getText().toString();
startSub.putExtra("name", input);
startActivity(startSub);

This starts your sub-activity. Now in the subActivity, you can retrieve these values in your onCreate() method like so:

Bundle extras = getIntent().getExtras();
    // make sure there is actually extra data
    if (extras != null) {
        String theName = extras.getString("name");
        TextView text = (TextView) findViewById(R.id.yourTextView);
        text.setText(theName);
    }

When you go back, and re-enter a new name the Intent will be created again, which means your textfield will get changed again.

Good luck!

Sander van't Veer
  • 5,930
  • 5
  • 35
  • 50
  • Outstanding! Now for a tricky bit. Let's say instead on setText i want to use append(). Let's say that activity 2 is actually a log module of some kind so passing data between activities could get quite bloated. Not true? If so, how could i then, say, save the state of activity 2? – rameezk Dec 07 '11 at 12:27
  • That can be done by using `startActivityForResult`.This creates an activity which sends data back after finishing.You could pick up this data in your main class,save it,and append it to the previous value. Empty string ->fill with edittext data -> sent to activity 2 -> send back on finish -> fill the earlier string -> append with new data -> repeat Atleast that's how I picture it in my mind, but I'm sure you have different plans. More information about how to send data back and forth using this method can be found here: http://www.vogella.de/articles/AndroidIntent/article.html#explicitintents – Sander van't Veer Dec 07 '11 at 12:36
  • Hmm after giving your question a quick think-through I think you might actually be looking for something like this: http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state/ This saves the application's state, which you can reuse when the activity get's reopened. That might be a better way of saving the data for your second class if you don't want to send the data back to 1 and then back to 2 all the time. – Sander van't Veer Dec 07 '11 at 12:41
  • Your effort has been a great help! Thank you. Would it be possible to just append data to the text view on the second activity while the first activity is in focus? – rameezk Dec 07 '11 at 14:15
  • I am inclined to say Yes, but I've actually never tried this myself since I like to see the results when I change them. But I'm pretty sure it's possible by using the `TextView text = (TextView) findViewById(R.id.yourTextView); text.setText(theName);` part in your first activity instead of in the second. It's worth a try! – Sander van't Veer Dec 07 '11 at 14:18
0

You have to ways you can do that. You can either pass a data throught an intent with:

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("name", nameText);

another approach is using the sharedpreferences which can be accessed throught your entire app.

If you want the back button to not close your activity you need to override the backButtonPressed method and start activityA again (Use the intent flags for not starting new instances all the time).

As for saving activity state read this: http://developer.android.com/guide/topics/fundamentals/activities.html#SavingActivityState

Raz
  • 8,918
  • 3
  • 27
  • 40