There is a Mainmenu class, there is a setActivityResult method, there is a second Newgame class, there is setResult(Request_state_1) which is 0, there is a button in xml it is android:enabled="false" , how to make the application close when true setActivityResult . I try to save via SheredPreference , but it doesn't work, what am I doing wrong?

Here is the code:

 public class Mainmenu extends AppCompatActivity { ... public static final String SaveResult = "saveresult"; public static final String SaveResultKey = "saveresultkey"; static final int Request = 1; int Request_state_1 = 1; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainmenu); btn = (Button) findViewById(R.id.bcontinue); ... SharedPreferences settings = getSharedPreferences(SaveResult, Context.MODE_PRIVATE); Request_state_1 = settings.getInt(SaveResultKey, Request_state_1); if (Request_state_1==0){ btn.setEnabled(true); btn.setTextColor(0xffffffff); } } @Override protected void onDestroy() { super.onDestroy(); SharedPreferences settings = getSharedPreferences(SaveResult, Context.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putInt(SaveResultKey, Request_state_1); editor.apply(); } public void onActivityResult(int requestcode, int resultCode, Intent intent) { super.onActivityResult(requestcode, resultCode, intent); if (requestcode == Request){ if (Request_state_1==0) { btn.setEnabled(true); btn.setTextColor(0xffffffff); } } } 

    3 answers 3

    Found a solution. Replaced Request_state_1 with RESULT_OK

     public void onActivityResult(int requestcode, int resultCode, Intent intent) { super.onActivityResult(requestcode, resultCode, intent); if (requestcode == Request){ if (resultCode==RESULT_OK) { btn.setEnabled(true); btn.setTextColor(0xffffffff); } } @Override protected void onCreate(Bundle savedInstanceState) { ... SharedPreferences settings = getSharedPreferences(SaveResult, Context.MODE_PRIVATE); Request_state_1 = settings.getInt(SaveResultKey, RESULT_OK); if (Request_state_1==RESULT_OK){ btn.setEnabled(true); btn.setTextColor(0xffffffff); } } 

      With a Destroy, you save the unit, then for onCreate reason in onCreate check for zero

        if (Request_state_1==0){ btn.setEnabled(true); btn.setTextColor(0xffffffff); } 

      Maybe Request_state_1==1 ?

        Something tells me that onDestroy not the best place to save.
        Save as soon as you change the UI in onActivityResult .