Reputation: 1485
I have a main activity included the button and textview. When I click the button, it will display a listview and fill some data into that listview. In the listview, I click a item, I want to send the item data to textview in main activity. However, it has crash. I have no idea to find the problem and log (does not show). Could you look at my code and provide me the solution?
MainActivity
btnManage = (Button) findViewById(R.id.btnManage);
btnManage.setText(manageLabel);
btnManage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
Intent intent = new Intent(v.getContext(), ListViewActivity.class);
intent.putExtra("List_data", "Hello");
v.getContext().startActivity(intent);
}
});
In ListViewActivity I have
Bundle bundle = getIntent().getExtras();
String data_String= bundle.getString("List_data");
ArrayList<String> data = new ArrayList<String>();
data.add(data_String);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, data);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
//Click one item
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String data_send = (String) mainListView.getItemAtPosition(position);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Data_Send", data_send);
startActivity(intent);
}
In the onCreate function in MainActivity, I have
Bundle bundle = getIntent().getExtras();
String data_activity =bundle.getString("Data_Send");
The Manifest file
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ListViewActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
/>
Upvotes: 0
Views: 2244
Reputation: 5361
In ListViewActivity you are trying to start MainActivity which is already started hence the error.
You need to finish ListViewActivity acivity so it just goes back to the previous activity which is the MainActivity and pass data
in ListViewActivity you finish the activity and pass data back like this
Intent intent = new Intent();
intent.putExtra("Data_Send", data_send);
setResult(RESULT_OK, intent);
finish();
in MainActivity you start the ListViewActivity like this
Intent i = new Intent(getApplicationContext(), ListViewActivity.class);
i.putExtra("List_data", "Hello");
startActivityForResult(i, 1);
also in MainActivity you need to get data back from the ListViewActivity so you add
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String data = data.getStringExtra("Data_Send");
// do whatever with data string
}}};
The if (requestCode == 1) {
is the number id you give when you start an activity with startActivityForResult(i, 1);
. If you want to do the same with another activity you can do startActivityForResult(i, 2);
and to get back data you check for id 2
if (requestCode == 2) { ...
Upvotes: 2
Reputation: 3212
You can use startActivityForResult for this purpose. When you call Activity.startActivityForResult(), you set the requestCode. Later, this request code is needed by onActivityResult() in order to determine what Activity is sending data to it. We don't need to supply requestCode again on setResult() because the requestCode is carried along.
The data is intent data returned from launched intent. We usually use this data when we set extras on the called intent.
Consider this example:
CALL SECOND ACTIVITY
Intent i = new Intent(MainActivity.this, CheckActivity.class);
startActivityForResult(i, REQUEST_CODE_CHECK);
ON SECOND ACTIVITY, SET INTENT RESULT
getIntent().putExtra("TADA", "bla bla bla");
setResult(RESULT_OK, getIntent());
finish();
BACK TO FIRST ACTIVITY, ONACTIVITYRESULT()
if(requestCode == REQUEST_CODE_CHECK && resultCode == RESULT_OK){
text1.setText(data.getExtras().getString("TADA") );
}
There you go. You should now understand what is Intent data and how to set and fetch the value.
Upvotes: 0
Reputation: 695
You should have a look at the following guide which outlines how to start an activity, and then wait for a result.
The problem with the way you are currently doing is that the "back stack" has 2 references to the main activity - i.e. hitting back will take you to the list activity, then to main activity again.
The advantage of using startActivityForResult()
allows you to separate out the logic of creating the MainActivity
, and handling the result of the ListViewActivity
.
Upvotes: 1
Reputation: 54672
startActivityForResult
. then when the list item is clicked then you can send result to the main activity. to know more about it you can check the doc
Upvotes: 1