-2

내 활동에는 전환 버튼이 있습니다. 앱이 백그라운드에서 닫히면 전환 버튼 상태를 유지하고 싶었습니다.응용 프로그램이 닫힌 상태에서 공유 환경 설정을 통해 스위치 버튼 상태 저장

스위치 상태는 앱이 배경에있을 때까지 유지되지만 앱이 배경에서 삭제되면 다시 기본 (OFF) 상태로 돌아갑니다.

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에서 스위치가 꺼져 있다는 것입니다. 따라서 앱을 다시 시작할 때마다 스위치는 Sharedpreference에 관계없이 OFF가됩니다.

다운로드 폴더에 PDF가 다운로드되었는지 확인하려면 추가했습니다. 만약 그렇다면 스위치를 ON으로 설정하고 설정하지 않으면 OFF로 설정합니다.

@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.apply() 대신 editor.commit()을 사용해보십시오. 차이점은 post을 참조하십시오.

+0

editor.commit()을 사용해도 여전히 상태가 저장되지 않습니다.이 경우 앱을 다시 열면 스위치가 다시 기본 상태 (OFF)로 돌아갑니다. –

+0

@Suhail Parvez가 앱 스위치를 죽이기 전에 말했습니까? 모드가 해제되어 있습니다. 다시 열면 onmode.am로 바뀝니다. – Vishwa

+0

@Vishwa 반대쪽에서 On 모드로 유지하고 배경을 찾아 다시 방문하십시오. 꺼짐 모드입니다. –