2

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.

duckduckgo
  • 1,280
  • 1
  • 18
  • 32

3 Answers3

4

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
   }
}
larsaars
  • 2,065
  • 3
  • 21
  • 32
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
2

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);  

                     }  

 }  
mujeeb.omr
  • 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
0

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