How can I notify (For example call to a method) of a parent activity that started another activity as an intent, so once second intent has done some work it should notify first activity passing some parameters.
-
1http://developer.android.com/training/basics/intents/result.html – Simba Nov 03 '14 at 04:18
-
wow thats an awesome but less told thing. – duckduckgo Nov 03 '14 at 04:23
-
You may want to look at this answer: http://stackoverflow.com/a/10407371/513413 – Hesam Nov 03 '14 at 04:26
3 Answers
Its very simple, you should call
startActivityForResult(yourNextActivityIntent,requestCode);
and then in next Activity you can send result to ParentActivity like this
setResult(RESULT_OK);
finish();
and in your parent Activity you have to override this method to get pending results
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// here you can check for the requestCode that you have requested while calling next Activity
if (requestCode == yourRequestCode && resultCode == RESULT_OK){
//perform your actions
}
}

- 2,065
- 3
- 21
- 32

- 4,051
- 3
- 27
- 39
When you finishing your activity
@Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});
Try this code in your parent activity, Override onActivityResult() Methode
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}

- 499
- 2
- 12
-
thanks for prompt answer, just one step of starting this activity is missing in this answer, so i chose another answer as best. – duckduckgo Nov 03 '14 at 04:29
We need to start activity by startActivityForResult(intent,resultcode) so this will carry two arguments as request code(INTEGER) and the intent. when the second activity completes the work we need to create intent without arguments then we need to invoke setResult(resultcode,intent) method to notify first activity. then we need to invoke finish() method to destroy second activity. when first activity comes into focus onActivityResult will be called so we need to implement this. there we can notify the first activity.
http://www.javatpoint.com/android-startactivityforresult-example

- 438
- 3
- 17