Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data from one activity to another in Android [duplicate]

Tags:

android

Possible Duplicate:
How to pass a value from one Activity to another in Android?

I have an activity with a list of titles and their bodies(content) (list6 example from ApiDemos). And I have an activity, where I add a note. When I click on "Add" button, I want the title of my note to appear on the list. Same with body. The problem is, that in List activity there are two String[] arrays with predefined hard-coded titles and bodies. What should I do to be able to add my own new titles with a content instead of having these hard-coded values? I know I must use intents with startActivityForResult, but that's probably all I know...

like image 649
lomza Avatar asked Mar 31 '11 09:03

lomza


People also ask

How pass data from one activity to another activity in Android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do you pass data from one activity to another activity using intent?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);

How do I transfer data from one Android app to another?

Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type. The system automatically identifies the compatible activities that can receive the data and displays them to the user.


2 Answers

You have two option:

1) make the listview and the two arraylists static

This way you can use the same instances of it from the activity with add button and modify the listview as:

FirstActivity.listTitlesArrayList.add(listTitleString);
FirstActivity.listDescriptionArraylist.add(listDescriptionString);//this is probably your note
FirstActivity.listView.invalidateViews();

2)if you want to use intents:

while going to the ListActivity pass data by..

intent.putExtra("Title", listTitleString);
intent.putExtra("Content", listDescriptionString);
startActivity(intent);

and to recover it in second activity use:

title= getIntent().getExtras().getString("Title");

...and so on..

like image 86
Vicky Kapadia Avatar answered Sep 29 '22 03:09

Vicky Kapadia


There are several ways to share data between activities.

In your case probably the easiest way is to have a reference to list in Application. This answer summs it up nicelly: Making data obtained in one activity available to all the activities

like image 23
Peter Knego Avatar answered Sep 29 '22 02:09

Peter Knego