31

I am confused in how to trigger an event when the activity is changed. One thing that I know is using onWindowFocusChanged() method but I don't know how to use it. Please help in one example i.e. when the activity changes, it should show a toast said "Activity changed".

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
abuybuy
  • 799
  • 2
  • 16
  • 33

4 Answers4

43

The following method gets called when window's focus changes. For instance, if the window gains/losses focus, it will be triggered. In case, the window gains focus, hasFocus is true and false otherwise.

 public void onWindowFocusChanged(boolean hasFocus) {         
     super.onWindowFocusChanged(hasFocus);
     if(hasFocus) 
         Toast.makeText(context, text, duration).show();
 }
galcyurio
  • 1,776
  • 15
  • 25
Reno
  • 33,594
  • 11
  • 89
  • 102
  • can you provide the full code of this function? because when i used this code, there's still an error there. – abuybuy Oct 28 '11 at 07:57
  • What kind of error? This is a callback so you can add whatever you want to do here. – Reno Oct 28 '11 at 08:07
  • Multiple marker at this line -Syntax error on token "boolean", delete this token -hasFocus cannot be resolved -Syntax error, insert ";" to complete Statement i am using Eclipse IDE – abuybuy Oct 28 '11 at 08:13
  • 5
    what if you need to use it in a fragment? – android developer Dec 24 '13 at 09:58
  • 2
    I can not understand what is gains or loses focus means ? – Olkunmustafa Mar 24 '16 at 18:06
  • Please writer the answer in more clear and precise way. Can't understand anything. – CopsOnRoad Oct 09 '17 at 15:54
  • The IF statement here is redundant - the callback is only fired when the window has focus. You can use the IF statement to see if the window is no longer focused to see when an activity changes though. On a side note, onWindowFocusChanged is called AFTER onCreate(). Knowing this may come in handy. – FoxDonut May 20 '19 at 22:35
4
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if(!hasFocus) {
            //do anything you want here
            Toast.makeText(MainActivity.this,"Activity changed",Toast.LENGTH_SHORT).show();
        }
    }

}

This might help...

M.Usman
  • 2,049
  • 22
  • 25
3

Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is visible to the user. The default implementation clears the key tracking state, so should always be called.

@Override
public void onWindowFocusChanged(boolean hasFocas) {
    super.onWindowFocusChanged(hasFocas);
    //your code here
}

hasFocus boolean: Whether the window of this activity has focus.

Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
2

Android 10 brought restrictions on apps accessing the clipboard. Now, only the app that currently has focus can access the clipboard.

Previously, you might be accessing this data in onResume, but on Android 10 this doesn’t work. Instead, you can wait until the view is laid out using onWindowFocusChangedcallback.