Search code examples
androidandroid-intentandroid-event

How to implement the call back mechanism between Activites


I have 2 activities first details activity and second activity page is confirmationPage,Once i confirm it should come back to first page,how should i handle this scenario?

Is it possible between activities,instead of using fragments?


Solution

  • you have to use startActivityForResult(); method when you start the secondActivity.

    and you also have to implement the onActivityResult() method.

    Here is the code for first Activity..

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
    
            if (resultCode == 1) {
                Bundle extra = data.getExtras();
                String ID = extra.getString("NameKey").trim();
                 // do your code here.
                
            }
        }
    

    Code in the second Activity..

    add the code on confirm click button.

      Intent i = new Intent();
      Bundle extra = new Bundle();
      extra.putString("NameKey", KeyValue);
                    
      i.putExtras(extra);
      setResult(1, i);
      finish();