intent method (onActivityResult) wipeout previous listview stored data - java

I am trying to create a group of phone contacts in CreateGroup.java . So, for that purpose, I go to a new activity named ContactsView.java using an Intent. There I a get list of all contacts. When I click a contact from that list, it gets the name of that contact item and returns it back to the previous activity named CreateGroup.java. But when I try to add more contacts, it is replacing the previous added item in the listview. I am unable to add more than one contact in my list.
Please someone help me!!
CreateGroup.java
public class CreateGroup extends ActionBarActivity {
TextView textView;
ListView show;
ArrayList<String> addArray=new ArrayList<String >();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_group);
ImageButton next = (ImageButton)findViewById(R.id.imgButtonAddContacts);
textView=(TextView)findViewById(R.id.textViewtst);
show= (ListView) findViewById(R.id.listView2);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), ContactsView.class);
startActivityForResult(myIntent, 0);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (0) : {
if (resultCode == Activity.RESULT_OK) {
String newText = data.getStringExtra("CName");
addArray.add(newText);
ArrayAdapter<String>adapter=new ArrayAdapter<String> (CreateGroup.this,android.R.layout.simple_list_item_1,addArray);
show.setAdapter(adapter);
}
break;
}
}
}
}
here is ContactsView.java
public class ContactsView extends ActionBarActivity {
String namecsv="";
String phonecsv="";
String namearray[];
String phonearray[];
ListView lv1;
ArrayList<String> list_items= new ArrayList<String>();
//declare a variable for counting of selected items.
int count=0;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contacts_view);
lv1=(ListView)findViewById(R.id.listView);
list_items.add("one");
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext()){
String name= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phonenumber= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(name!=null){
namecsv+=name + ",";
phonecsv+=phonenumber + ",";
}
}
phones.close();
namearray= namecsv.split(",");
phonearray=phonecsv.split(",");
final ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,android.R.layout.simple_list_item_1,android.R.id.text1,namearray);
lv1.setAdapter(adapter);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String msgname = namearray[position];
String msgnum = phonearray[position];
Toast.makeText(getApplicationContext(), msgname + " " + msgnum, Toast.LENGTH_SHORT).show();
Intent resultIntent = new Intent();
resultIntent.putExtra("CName", msgname);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
}
}
I have updated my code according to the answer provided. but still facing the same problem. its only show only on last add item in the listview. previous item removes.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case (0) : {
if (resultCode == Activity.RESULT_OK) {
String newText = data.getStringExtra("CName");
textView.setText(newText);
addArray.add(newText);
adapter.notifyDataSetChanged();
show.setAdapter(adapter);
}
break;
}
default:
super.onActivityResult(requestCode, resultCode, data);
}
}

You should not call super.onActivityResult(requestCode, resultCode, data); as very first thing, there but only when you face requestCode that is not handled by your code. In other words, this should be added to switch block, in default section. Other thing is that you should not set new adapter each time. So this:
addArray.add(newText);
ArrayAdapter<String>adapter=new ArrayAdapter<String> (CreateGroup.this,android.R.layout.simple_list_item_1,addArray);
show.setAdapter(adapter);
should be replaced by:
addArray.add(newText);
adapter.notifyDatasetChanged();
which simply adds data to your current array then notify the adapter (which you need to create in onCreate() additionally of course) about the change in dataset.

Related

Duplicate Items In ArrayList Android Studio

I am receiving the variable from a different activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check that it is the SecondActivity with an OK result
if (requestCode == 0) {
if (resultCode == RESULT_OK) { // Activity.RESULT_OK
// get String data from Intent
String returnString = data.getStringExtra("keyName");
// Add data to ArrayList
Array_List.add(1,returnString);
}
}
}
Expected Result:
Item Entered by user
Result:
Duplicates the last added item on the ArrayList
public class MainActivity extends AppCompatActivity {
public final String FILENAME = "example.txt";
ArrayList<String> Array_List = new ArrayList<String>();
Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv = (ListView) findViewById(R.id.LV_Main);
Array_List.add(0,"Saved Posts");
//Ensures Add new is always at the end
Array_List.add(Array_List.size(), "Add New +");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.activity_list_item,android.R.id.text1,
rray_List );
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check that it is the SecondActivity with an OK result
if (requestCode == 0) {
if (resultCode == RESULT_OK) { // Activity.RESULT_OK
// get String data from Intent
String returnString = data.getStringExtra("keyName");
// Add data to ArrayList
Array_List.add(1,returnString);
}
}
}
}
Thanks in advance
Edit:
Array_List.add(Array_List.size() - 1, "Add New +");
Does not work
Also, I need Add New to always be at the bottom and Saved Posts at the top
It is weird as if I remove the Array_List.add(1,returnString);
Then it works but adds the returnstring to the bottom of the ArrayList, however I need the Add New always at the bottom
The behaviour you're looking for already exists in ListView - it's called a "Header" and a "Footer". You can also just define one in xml to make your life easier:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv = (ListView) findViewById(R.id.LV_Main);
lv.addHeaderView(createTextView("Saved Posts"));
lv.addFooterView(createTextView("Add New +"));
//Etc. etc.
}
private TextView createTextView(String content) {
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tv.setText(content);
return tv;
}
Now you don't need to worry about inserting the first and last elements yourself - just add the content to your list and update your adapter with it.
Edit: If you want the header and footer to have the same styling as the rest of your list elements:
private View createHeaderOrFooter(String content) {
final LayoutInflater inflater = LayoutInflater.from(this);
final View row = inflater.inflate(android.R.layout.activity_list_item, null);
((TextView)row.findViewById(android.R.id.text1)).setText(content);
return row;
}
replace
Subreddit_Array_List.add(Subreddit_Array_List.size(), "Add New +");
with
Subreddit_Array_List.add(Subreddit_Array_List.size() - 1, "Add New +");

Android - Adding item to ListView from another Activity

First of all, I've searched on many other posts and still not found a fix for it.
MainActivity contains a ListView and an ImageButton that takes to AddActivity.
This AddActivity has got a EditText (nameAddInput) and a Button(addButton).
Despite clicking this Button, the ListView in MainActivity remains empty... Don't understand why...
Here is the the code of MainActivity:
public class MainActivity extends AppCompatActivity {
static final int PICK_CONTACT_REQUEST = 0;
private ListView list;
private ArrayAdapter<String> adapter;
private ArrayList<String> arrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.itemsList);
arrayList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, R.layout.listview_style1, android.R.id.text1, arrayList);
}
public void onClickAddButton(View view) {
Intent i = new Intent(MainActivity.this, AddActivity.class);
startActivityForResult(i, 2);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
addNewItem();
}
}
}
public void addNewItem() {
Bundle addNameInfo = getIntent().getExtras();
if(addNameInfo == null)
return;
String nameInput = addNameInfo.getString("nameInput");
arrayList.add(nameInput);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
In xml file of MainActivity in the ImageButton: android:onClick="onClickAddButton"
The code of AddActivity:
public class AddActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
}
public void backToMain(View view) {
Intent i = new Intent();
EditText nameAddInput = (EditText) findViewById(R.id.nameAddInput);
String userNameText = nameAddInput.getText().toString();
i.putExtra("nameInput", userNameText);
setResult(RESULT_OK, i);
finish();
}
}
In xml file of AddActivity in the Button: android:onClick="backToMain"
Hope someone can help!!
Thank you in advance!!
getIntent() returns you the intent that launched MainActivity, not the one you set in backInMain
Try the "data" variable passed to you in onActivityResult?
Also change to
startActivityForResult(i, PICK_CONTACT_REQUEST);
I'd also suggest you rename that variable 😉
set adapter just in onCreate and just call notifyDataSetChanged in addNewItem method.
the resulting value is returned in the Intent data-Parameter of the onActivityResult function, not in the intent-member of the mainActivity.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
addNewItem(data.getExtras().getString("nameInput");
}
}
}
...
public void addNewItem(String newItem)
...

onActivityResult requestCode returns 0

I have MainActivity with a fragment consisting of two tabs, with one fragment in each tab (ChatListFragment and FriendListFragment). Each fragment has ListView. When user tap on each row of ListView, ChatActivity will show.
I wanted ListView (with ListAdapter as adapter that calls ChatActivity) in ChatListFragment to update its ListView once ChatActivity closes. I have tried onActivityResult method as shown below in my code but requestCode is always -1.
MainActivity.java
public class MainActivity extends AppCompatActivity {
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: " + " " + requestCode + " == " + MYACTIVITY_REQUEST_CODE + " and " + resultCode + " == " + Activity.RESULT_OK);
Log.d(TAG, "onActivityResult: " + MainActivity.class.getSimpleName());
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
...
}
ChatListFragment.java
public class ChatListFragment extends Fragment {
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: " + TAG);
Log.d(TAG, "onActivityResult: " + " " + requestCode + " == " + MYACTIVITY_REQUEST_CODE + " and " + resultCode + " == " + Activity.RESULT_OK);
if ((requestCode == MYACTIVITY_REQUEST_CODE) && (resultCode == Activity.RESULT_CANCELED)) {
List<Friend> friendList = getFriendChatList();
adapter = new ListAdapter(getActivity(), R.layout.fragment_list, friendList, true);
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
Log.d(TAG, "onActivityResult number of friend chats: " + getFriendChatList().size());
if(friendList.isEmpty()) {
linearLayout.setVisibility(View.VISIBLE);
} else {
linearLayout.setVisibility(View.GONE);
}
}
}
}
ChatActivity.java
public class ChatActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
if (getParent() == null) {
setResult(Activity.RESULT_OK);
} else {
getParent().setResult(Activity.RESULT_OK);
}
}
...
}
ListAdapter.java
public class ListAdapter extends ArrayAdapter<Friend> {
private static final int MYACTIVITY_REQUEST_CODE = 9000;
public ListAdapter(Context context, int resource, List<Friend> objects, boolean isChat) {
super(context, resource, objects);
this.context = context;
}
#Override
public View getView(final int position, final View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(null == view) {
view = layoutInflater.inflate(R.layout.list_friend, null);
}
final Friend friend = friendList.get(position);
if(null != friend) {
final CardView rowLayout = (CardView) view.findViewById(R.id.row_layout);
final ImageView friendImage = (ImageView) view.findViewById(R.id.list_friend_image);
final TextView friendName = (TextView) view.findViewById(R.id.list_friend_name);
final TextView friendDescription = (TextView) view.findViewById(R.id.list_friend_description);
final TextView friendLastChatTime = (TextView) view.findViewById(R.id.list_friend_lastChatTime);
// Open chat activity
rowLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(KEY_FRIEND_NAME, friend.getName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
((Activity) context).startActivityForResult(intent, MYACTIVITY_REQUEST_CODE);
}
});
}
return view;
}
}
You need to make an Intent and then set the Result to that Intent.
Try something like this in your ChatActivity
Intent intent=new Intent();
if (getParent() == null) {
setResult(Activity.RESULT_OK,intent);
} else {
getParent().setResult(Activity.RESULT_OK,intent);
}
finish();
You also need to finish your ChatActivity after setting the result. Like this:
What I don't understand, is why you need to check for parent class. You can always simply do like this
Intent intent= new Intent();
setResult(RESULT_OK,intent);
finish();
And this should work properly.
Hope this helps :)
I know this is really late! But, the onActivityResult will not get the result which you set in setResult()!
Because you set Intent.FLAG_ACTIVITY_NEW_TASK in rowLayout.setOnClickListener
Based on reference
This flag can not be used when the caller is requesting a result from
the activity being launched.
SO, first of all, remove intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Then setResult(RESULT_OK,intent); in appropriate place
In this situation, you will get the response which you set, in onActivityResult

Go back to previous Activity with some `put extra` onClick of a recyclerView Item

I want to pass an data previous activity on click of Item in Recycler view and show it on a Edit Text.
This is the code i have used to pass data from listview to the previous activity
I want to do the same thing with Recyclerview
//Calling Second Activity
public static final int REQUEST_CODE = 100;
Intent dateintent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(dateintent, REQUEST_CODE);
//onClick of listview pass the data back to previous activity
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt = (TextView) view.findViewById(R.id.textView);
String str = txt.getText().toString();
Intent intent = new Intent();
intent.putExtra("data",str);
setResult(RESULT_OK,intent);
finish();
}
});
//After getting data show the data in the first activity edit box
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String data= data.getStringExtra("data");
if (data!= null) {
edittext.setText(data);
}
}
}
}
First create this Interface
public interface RunnableValue {
public void run(Object obj);
}
2.This MainActivity add
RunnableValue run=new RunnableValue() {
#Override
public Bundle run(Object obj) {
String str = obj.toString();
Intent intent = new Intent();
intent.putExtra("data",str);
setResult(RESULT_OK,intent);
finish();
}
};
mAdapter = new SearchAdapter(dataSet,run);
This RecyclerView Adapter
public SearchAdapter(List<String> dataSet,RunnableValue runnableValue) {
mDataSet = dataSet;
this.runnableValue=runnableValue;
}
public static class SearchHolder extends RecyclerView.ViewHolder {
private final TextView textView;
public SearchHolder(View v) {
super(v);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
runnableValue.run(getTextView().toString());
}
});
textView = (TextView) v.findViewById(R.id.txtSearchItem);
}
public TextView getTextView() {
return textView;
}
}
Follow the Jacob's solution here. This adds listener for RecyclerView. Then, do the same as you have done in ListView.
There is no setOnItemClickListener available in RecyclerView, so you need make your own click listener in your RecyclerView adaper, just check out the post, then you should be able to make it.
Hope this help!
RecyclerView doesn't have a setOnItemClickListener like its predecessor ListView did. However, that shouldn't prevent us from doing what we want to do. So, we reinvent the wheel and make our very own OnItemClickListener for your RecyclerView. Here's a step by step guide.
Create an interface called OnItemClickListener by creating a new file called OnItemClickListener.java with an empty method called onItemClick.
public interface OnItemClickListener {
public void onItemClick(View view , int position);
}
Create a static variable in your adapter called
static OnItemClickListener mItemClickListener;
Setup onClickListener in your custom ViewHolder with a call to our onItemClick method like so
#Override
public void onClick(View view) {
mItemClickListener.onItemClick(view, getPosition());
}
Create a public method called SetOnItemClickListener in your adapter class
public void SetOnItemClickListener(final OnItemClickListener mItemClickListener)
{
this.mItemClickListener = mItemClickListener;
}
SetOnItemClickListener on your custom RecyclerView Adapter
((NameOfYourAdapter) mAdapter).SetOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
if(view != null)
{
TextView txt = (TextView) view.findViewById(R.id.textView);
String str = txt.getText().toString();
Intent intent = new Intent();
intent.putExtra("data",str);
setResult(RESULT_OK, intent);
//close this Activity...
finish();
}
}
});
That should do it. If you have any questions, feel free to ask!

Returning Data Result to Parent Activity using Intents

I am able to successfully transfer the string in my ListView in my first Activity to the EditText in my second Activity. I now want to edit the text and send it back to update my ListView in my first Activity. I basically want to have the edits be sent back to the first activity as a popup to help me test which string is being passed back
I'm not sure what Intent to put in my onActivityResult():
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String name = data.getExtras().getString("name");
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
}
}
Here is my first Activity:
public class ToDoActivity extends Activity {
private ArrayList<String> todoItems;
private ArrayAdapter<String> todoAdapter; // declare array adapter which will translate the piece of data to teh view
private ListView lvItems; // attach to list view
private EditText etNewItem;
private final int REQUEST_CODE = 20;
//private Intent i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
etNewItem = (EditText) findViewById(R.id.etNewItem);
lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView
//populateArrayItems(); // call function
readItems(); // read items from file
todoAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems); //create adapter
lvItems.setAdapter(todoAdapter); // populate listview using the adapter
//todoAdapter.add("item 4");
setupListViewListener();
setupEditItemListener();
onActivityResult(REQUEST_CODE, RESULT_OK, /** Intent variable **/);
}
private void launchEditItem(String item) {
Intent i = new Intent(this, EditItemActivity.class);
i.putExtra("itemOnList", item); // list item into edit text
//startActivityForResult(i, REQUEST_CODE);
startActivity(i);
}
private void setupEditItemListener() { // on click, run this function to display edit page
lvItems.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View item, int pos, long id) {
String text = (String) lvItems.getItemAtPosition(pos);
launchEditItem(text);
}
});
}
private void setupListViewListener() {
lvItems.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> adapter, View item, int pos, long id) {
todoItems.remove(pos);
todoAdapter.notifyDataSetChanged(); // has adapter look back at the array list and refresh it's data and repopulate the view
writeItems();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.to_do, menu);
return true;
}
public void onAddedItem(View v) {
String itemText = etNewItem.getText().toString();
todoAdapter.add(itemText); // add to adapter
etNewItem.setText(""); //clear edit text
writeItems(); //each time to add item, you want to write to file to memorize
}
private void readItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
todoItems = new ArrayList<String>(FileUtils.readLines(todoFile)); //populate with read
}catch (IOException e) { // if files doesn't exist
todoItems = new ArrayList<String>();
}
}
private void writeItems() {
File filesDir = getFilesDir(); //return path where files can be created for android
File todoFile = new File(filesDir, "todo.txt");
try {
FileUtils.writeLines(todoFile, todoItems); // pass todoItems to todoFile
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
String name = data.getExtras().getString("name");
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
}
}
}
I thought about using the Intent from the second activity but I'm not sure how to do so.
Here is my second Activity.
public class EditItemActivity extends Activity {
private EditText etEditItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_item);
Intent i = getIntent();
String ItemToEdit = i.getStringExtra("itemOnList");
etEditItem = (EditText)findViewById(R.id.etEditItem);
etEditItem.setText(ItemToEdit);
onSubmit(etEditItem);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.edit_item, menu);
return true;
}
public void DoneEdit(View v) {
this.finish();
}
public void onSubmit(View v) {
EditText etName = (EditText) findViewById(R.id.etEditItem);
Intent data = new Intent();
data.putExtra("EditedItem", etName.getText().toString());
setResult(RESULT_OK, data);
finish();
}
}
To get result form an activity (child) you do as follow :
In the parent activity
startActivityForResult(myIntent, 1);
global vars of your parent activity
boolean backFromChild = false;
String aString;
then still in the parent activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// code for result
aString = getIntent().getExtras().getString("aString");
backFromChild = true;
}
if (resultCode == RESULT_CANCELED) {
// Write your code on no result return
}
}
}
in your child you do somewhere something like that
Intent returnIntent = new Intent();
//example of sending back a string to the parent.
returnIntent.putExtra("aString", aString);
setResult(RESULT_OK, returnIntent);
finish();
The thing is that onResume of your parent activity will be called when returning from your child. In there you have to perform the update, in your case it is to update the information of the edited text :
#Override
public void onResume(){
super.onResume();
if (backFromChild){
backFromChild = false;
//do something with aString here
Toast.makeText(this, aString, Toast.LENGTH_SHORT).show();
}
}
Basically, in the onActivityResult I get the info back from the intent of the child. Then in onResume I use this info.
For your concern you can utilize SharedPreferences
For ex: Put data in SP in second activity like this
SharedPreferences spppp = getSharedPreferences("tab", 0);
SharedPreferences.Editor editors = spppp.edit();
editors.putString("for", "0");
editors.commit();
and fetch data for list view in first activity like this
SharedPreferences spppp = getSharedPreferences("tab", 0);
String your_list_view_value = spppp.getString("for", "");

Categories