[英]Reloading or Recreating old activity after changing a Setting Android
我有一個顯示數據的活動,在一切開始之前,我要檢查是否有互聯網。 如果沒有,請向用戶發送Wifi設置。
我面臨的問題是,用戶確實連接到互聯網並按回去之后,該應用程序為空,沒有數據。 有沒有辦法重新加載此活動或重新創建它?
我嘗試了這段代碼,但是在進入設置之前它會重新創建活動,因此對話框保持打開狀態
@Override
protected void onResume() {
super.onResume();
this.onCreate(null);
}
這是檢查互聯網連接的代碼
if(!isNetworkAvailable()){
//Toast.makeText(getApplicationContext(), "internet is not avialable", Toast.LENGTH_LONG).show();
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setMessage("There is no Internet Connection.\n Please check your settings.");
ad.setCancelable(false);
ad.setPositiveButton("Internet Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
}
});
ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
finish();
}
});
ad.show();
}
也許最簡單的解決方案是調用Activity
類的recreate()
方法。 類似於以下內容: YourActivity.recreate()
。 請記住,這僅適用於> = API 11。
您還可以使用Intent
來重新啟動當前的Activity
,如下所示:
Intent intent = getIntent();
finish();
startActivity(intent);
您可以在此處找到更多信息: 如何重新啟動Android活動
在代碼的onResume
部分中,我添加了一個if statement
,用於再次檢查互聯網。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.