Q: Check how long time ago the app was focused I want to check how long time the app has been minimized with onWindowFocusChanged(boolean hasFocus) but can't figure out how.
This is what i've tried.
private long startTime = 0;
@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        if(hasFocus){
            long time = System.currentTimeMillis() - starTime;
            //300.000 ms = 5 min
            if(time > startTime + 300000)
            {
                Intent intent = new Intent(this, LoginActivity.class);
                startActivity(intent);
            }
        }
        else
        {
            starTime = System.currentTimeMillis();
        }
    }

I expect it to start counting when it loses focus. Then when it gains focus again, it should check if it was over 5 minutes or not. 

If false: Reset the timer/do nothing
If true: load the login activity.

A: To avoid problems start by setting initial value of startTime in onCreate method:
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startTime = System.currentTimeMillis();
    }

Then rename your variable long time to long timeDifference and when the name is more descriptive you'll see your condition is broken, so instead of your:
 if(timeDifference > startTime + 300000)

it will be enough to use:
 if(timeDifference > 300000)

Rememeber you can use debugging to analyze step by step current state of every variable.
