Coming back from intent, do something

I have an android application which is using listView. After clicking on any item on the list it shows you AlertDialog where you can chose either edit or delete. After clicking edit it starts a new intent sending some extra strings to fill up the form for editing. I would like to refresh the list after I click on button save in that EditActivity.

I have read about notifyDataSetChanged() and I think it could work I just would like to know if there is any method which is in MainClass (listView class) and it is executed right after I came back from intent, right after clicking save button in EditActivity.

Or may I just add notifyDataSetChanged() method after I start an activity in the MainActivity?

Upvotes: 0

Views: 484

Answers (4)

Ahmad Raza
Ahmad Raza

Reputation: 2850

public class yourClass extends ListActivity {

//define a class variable here.
private ArrayAdapter<Friend> adapter;


@Override
public void onCreate(Bundle savedInstanceState){

//Change this line  setListAdapter(new ArrayAdapter<Friend>(this, android.R.layout.simple_list_item_1, db.getAllFriends()));

to

adapter =  new ArrayAdapter<Friend>(this, android.R.layout.simple_list_item_1, db.getAllFriends());

 setListAdapter(adapter);
}


@Override
public void onResume(){
super.onResume();
adapter.notifyDataSetChanged();
}


}

Upvotes: 1

Farnabaz
Farnabaz

Reputation: 4066

use startActivityForResult to call your EditActivity (this isn't simplest way to do what you want but is good way to do), by using this you can find out what is the result of your edit

Intent i = new Intent(this, EditActivity.class);
startActivityForResult(i, 1);

In your EActivity set the data which you want to return back to MainActivity

 Intent returnIntent = new Intent();
 returnIntent.putExtra("result",YOUR_RESULT); // skip if you do not want to return special result
 setResult(RESULT_OK,returnIntent);     
 finish();

if you want to send cancel action (in your case fail edit) use this

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

Now in your MainActivity class write following code for onActivityResult() method

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  if (requestCode == 1) {

     if(resultCode == RESULT_OK){  
         String result=data.getStringExtra("result");   // result sent from EditActivity    
         listViewAdapter. notifyDataSetChanged();         
     }
     if (resultCode == RESULT_CANCELED) {    
         //Write your code if there's no result
     }
  }
}//onActivityResult

Upvotes: 1

S.Thiongane
S.Thiongane

Reputation: 6905

Use the method startActivityForResult() when opening your EditActivity then implement onActivityResult method in your main class. There you will be able to use notifyDataSetChanged

Read this from the doc

Upvotes: 1

Ahmad Raza
Ahmad Raza

Reputation: 2850

You can add listView.notifyDataSetChanged() in your activity onResume() function.

@Override
public void onResume(){
super.onResume();
yourListView.notifyDataSetChanged();
}

Upvotes: 1

Related Questions