-2

在我的活動中,我有一個切換按鈕。我想在應用程序從背景關閉時保持開關按鈕的狀態。當應用程序關閉時通過共享首選項保存切換按鈕狀態

當應用程序位於後臺時,交換機的狀態保持不變,但當應用從後臺清除時,交換機的狀態會回到默認(關閉)狀態。

我試着從here複製程序。但我仍然無法保持開關按鈕的狀態。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    switch1 = (Switch) findViewById(R.id.switch1); 

    SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyz", MODE_PRIVATE); 
    switch1.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true)); 


    switch1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (switch1.isChecked()) { 

       SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit(); 
       editor.putBoolean("NameOfThingToSave", true); 
       editor.apply(); 
       switch1.setChecked(true); 

       AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
       // Setting Dialog Title 
       alertDialog.setTitle("Download all the Product's PDF."); 
       // Setting Icon to Dialog 
       alertDialog.setIcon(R.drawable.pdf_alert_dialog); 

       // Setting Positive "Yes" Button 
       alertDialog.setPositiveButton("CANCEL", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           switch1.setChecked(false); 
           SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit(); 
           editor.putBoolean("NameOfThingToSave", false); 
           editor.apply(); 

           dialog.dismiss(); 
          } 
         }); 
       // Setting Negative "NO" Button 
       alertDialog.setNegativeButton("DOWNLOAD ALL ", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 

           SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit(); 
           editor.putBoolean("NameOfThingToSave", true); 
           editor.apply(); 


           AlertDialog.Builder alertDialog1 = new AlertDialog.Builder(context); 
           // Setting Dialog Title 
           alertDialog1.setTitle("Free storage Available:" + megAvailable1 + " MB"); 
           alertDialog1.setMessage("File size to Download: POJO MB"); 
           // Setting Icon to Dialog 
           alertDialog1.setIcon(R.drawable.pdf_alert_dialog); 

           alertDialog1.setPositiveButton("CANCEL", 
             new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog1, int which) { 
               switch1.setChecked(false); 
               SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit(); 
               editor.putBoolean("NameOfThingToSave", false); 
               editor.apply(); 

               dialog1.dismiss(); 
              } 
             }); 
           // Setting Negative "NO" Button 
           alertDialog1.setNegativeButton("DOWNLOAD ", 
             new DialogInterface.OnClickListener() { 
              public void onClick(DialogInterface dialog1, int which) { 

               getFeedDownload(); 

               SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit(); 
               editor.putBoolean("NameOfThingToSave", true); 
               editor.apply(); 

              } 

             }); 
           alertDialog1.show(); 
          } 
         }); 

       // Showing Alert Message 
       alertDialog.show(); 
      } else { 
      } 
     } 
    }); 

你能告訴我哪裏出錯了嗎?

回答

0

我做的錯誤是在我的onResume中關閉了Switch。 因此,每次我重新啓動應用程序時,無論Sharedpreference如何,開關都會關閉。

我添加了一個檢查,看看我是否下載了下載文件夾中的PDF。 如果他們是,那麼我只是把開關打開,如果沒有,那麼,關閉。

@Override 
public void onResume() { 
    super.onResume(); 

    Call<List<Products>> listCall = mManager.getProductsService().getAllProducts(); 
    //execte for the call back (asynchronous). 

    // Now we start to execute the call 
    listCall.enqueue(new Callback<List<Products>>() { 
     @Override 
     public void onResponse(Response<List<Products>> response, Retrofit retrofit) { 
      if (response.isSuccess()) { 
       List<Products> productsList = response.body(); 

       for (int i = 0; i < productsList.size(); i++) { 
        Products products = productsList.get(i); 
        String links = (products.getFilePath()); 
        String name = products.getFileID(); 
        String link = links.replaceAll("\\\\", ""); 
        Log.i("linkkkkkSettings", link); 


        File applictionFile = new File(Environment.getExternalStoragePublicDirectory(
          Environment.DIRECTORY_DOWNLOADS) + "/" + name + ".pdf"); 
        if (applictionFile != null && applictionFile.exists()) { 

         switch1.setChecked(bundle.getBoolean("ToggleButtonState", true)); 
        } else { 

         switch1.setChecked(bundle.getBoolean("ToggleButtonState", false)); 
        } 


       } 
      } 
     } 

     @Override 
     public void onFailure(Throwable t) { 

     } 
    }); 


} 
1

也許試試editor.commit()而不是editor.apply()。請參閱此post以瞭解它們之間的差異。

+0

我也嘗試使用editor.commit(),但依然狀態沒有得到保存,因爲這一點,當我重新打開應用程序的切換返回到默認狀態(滅)。 –

+0

@Suhail Parvez做你說殺應用程序開關之前處於關閉模式。當你重新打開它它去onmode.am我 – Vishwa

+0

@Vishwa它的對面,我保持在On模式,當我關閉應用程序從背景並再次回來。它將處於關閉模式。 –