Вопрос

OnCreate of my "Home" activity, I want to check if there's internet connection, if false just close my activity showing a toast..
But, my Home Activity could not be the first on stack, so if just set finish(); it could just close this activity and show the top one in activity stack..
So I've written down this code, but does it make any sense?

 if(!Utils.isOnline(mContext))
        if(!moveTaskToBack(true))
            finish();

Where Utils.isOnline() is just my method to check internet connection

EDIT: I've already created my method to check internet connection and it's Utils.isOnline().. So I'm not asking how to check internet connectio...

EDIT2: moveTaskToBack() probably is not best choice to achieve my target, because yes it puts my activity onBackGround but if I reopen it, app doesn't check anymore my condition (Don't know why.. it skips onCreate(?)) and shows a blank activity..

Это было полезно?

Решение

If you want to close your app you can add this lines:

Intent intent = new Intent(Intent.ACTION_MAIN);
       intent.addCategory(Intent.CATEGORY_HOME);
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

Другие советы

Try this code.

put following condition in onCreate().

if(!Utils.isOnline(mContext)){


    Toast.makeText(getApplicationContext(), "You have no Internet Connection!", Toast.LENGTH_SHORT).show(); 
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
    finish(); 
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top