Marcanah
Marcanah

Reputation: 43

enable button from another activity

I want to enable my btn_test which i set in false. here is my code in mainactivity.java

  btn_test= (Button) findViewById(R.id.btn_test);
     btn_test.setEnabled(false);

then in another class, Changactivity.java i need to set a pattern first

if(password.equals(PATTERN_KEY)){
                Toast.makeText(getApplicationContext(), "Pattern created successfully!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(ChangeActivity.this, MainActivity.class);


                editor.putString("Pattern", password);
                editor.commit();

                startActivity(intent);
                finish();

how I can go back to Mainactivity.java and set the btn_test into true??

Upvotes: 2

Views: 1139

Answers (2)

Dejvid
Dejvid

Reputation: 470

In Changactivity.java after Intent intent = new Intent(ChangeActivity.this, MainActivity.class); add intent.putExtra("state", "success");.Then, in your MainActivity:

if(getIntent().hasExtra("state")){
  if (getIntent().getStringExtra("state").equals("success")){
  btn_test.setEnabled(true);
  }else{
  btn_test.setEnabled(false);
  }
}else{
  btn_test.setEnabled(false);
}

Upvotes: 3

Hitesh Gehlot
Hitesh Gehlot

Reputation: 1347

use startActivityForResult instead of startActivity

and enable button in MainActivity onActivityResult method.

Upvotes: 0

Related Questions