-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模式,当我关闭应用程序从背景并再次回来。它将处于关闭模式。 –

相关问题