0

In Activity A there is a variable with a value. From here user goes to Activity B then to Activity C. Here he does something that changes that variable in Activity A. So when user goes BACK from Activity C to Activity B to Activity A, that value must be changed. To make things simpler, all I want is to show a Toast message when user goes back (by clicking the BACK button) to Activity A without having to launch it again.

By changing a variable I mean when Activity A is displayed to the user it needs to know that something happened in Activity C that makes Activity A change the variable (by running a script or something).

The problem is that the Toast message is displayed when I launch Activity A, but it's not when I go back from Activity C to Activity B to Activity A.

Here is what I tried:

OnCustomEventListener.java

public interface OnCustomEventListener {
    public void onEvent(); 
}

Activity A:

@Override
public void onCreate(Bundle savedInstanceState) {
     Activity_YourProfile actv_yourprofile = new Activity_YourProfile();
     //endless lines of code
     actv_yourprofile.setCustomEventListener(new OnCustomEventListener(){
         public void onEvent(){
            Toast.makeText(Activity_MyProfile.this, "performed", Toast.LENGTH_SHORT).show();
         }
}); 

}

Activity B:

Nothing special just an intent that takes user to Activity C.

Activity C:

private OnCustomEventListener mListener;
@Override
    public void onCreate(Bundle savedInstanceState) {
        //code
        setCustomEventListener(mListener);  //calling callback e.g. on Btn click
    }
    public void setCustomEventListener(OnCustomEventListener eventListener) {
       this.mListener=eventListener;
       if(this.mListener!=null){
           this.mListener.onEvent();
       }
    }

Based on the solution of Mocialov Boris.

Community
  • 1
  • 1
erdomester
  • 11,669
  • 32
  • 129
  • 230
  • 2
    you can use `startActivityForResult` and override `onActivityResult` – Raghunandan Apr 21 '14 at 13:35
  • I want to use Callbacks. Btw, there is an Activity C between Activity A and Activity B, so I don't think startActivityResult would work in this situtation. – erdomester Apr 21 '14 at 13:42
  • if you say it won't work i have nothing to say. good luck – Raghunandan Apr 21 '14 at 13:44
  • What @Raghunandan said. Trying to use callbacks when you don't even know whether the objects exist (Activities may/will be destroyed by the system when they're not in view) is a questionable approach. The example doesn't make sense in the context of Android development either -- an Activity is what's visible. Pressing a button on one Activity should *not* have any effect on another, invisible, Activity. – 323go Apr 21 '14 at 14:05
  • Have you tried showing the toast on onResume method? I think your activity is no being recreated, just resumed once you are coming back from Activity B. – hmartinezd Apr 21 '14 at 14:27
  • @323go you misunderstand me. I don't want to change anything in an Activity that may not exist. I want to do something in that Activity when it is displayed again to the user. It needs to know that something happened in another activity and in that case change that variable. – erdomester Apr 21 '14 at 14:32
  • Then you might want to keep state elsewhere and restore in `onResume()` as intended by the framework designers. Once again, there's no guarantee that an Activity you've left behind will still be alive when another activity returns, especially once it sinks lower in the back stack. At best, your callbacks would be strong references that prevent the garbage collector from working, and you might run low on system resources with this sort of pattern. – 323go Apr 21 '14 at 14:38

0 Answers0