How to use onWindowFocusChanged() method?

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...


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

Tags:

Android