How to set a button visible from another activity in android

前端 未结 3 1005
北恋
北恋 2021-01-25 03:19

I have a very simple problem. I have a invisible button in my Main Activity, and I have a second Activity that makes that button visible. In the second activity I don´t have a p

相关标签:
3条回答
  • 2021-01-25 03:36

    Solve the problem. In the second Activity a have a static boolean variable that begins false. Once the oncreate() in the second Activity is call, the boolean variable is change to true. onresume() in MainActivity i check if the boolean variable is true, and change the visibility of the button.

    SecondActivity

    public static boolean showButton = false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showButton = true;
    
    }
    

    MainActivity

    @Override
    protected void onResume() {
        if ( Display.showButton){
            mMyButtonToBeHidden.setVisibility(View.VISIBLE);
        }
        super.onResume();
    } 
    
    0 讨论(0)
  • 2021-01-25 03:39

    Try this code in onResume() of your MainActivity:

     try {
         if(getIntent().getExtras().containsKey("your_parameter")) {
             btn.setVisibility(View.VISIBLE);
         }
     } catch(Exception e){
         //...
     }
    

    In second Activity put "your_parameter" parameter as extra to Intent only if you want to make that button visible.

    0 讨论(0)
  • 2021-01-25 04:00

    You should set up a communication between the 2 activities. You can achieve this with startActivityForResult() and onActivityResult()

    MainActivity:

    public class MainActivity extends Activity {
    
        public static final int REQUEST_CODE_SECOND_ACTIVITY = 100; // This value can be any number. It doesn't matter at all. The only important thing is to have the same value you started the child activity with when you're checking the onActivityResult.
        public static final String SHOW_BUTTON = "shouldShowButton";
    
        private Button mMyButtonToBeHidden;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            mMyButtonToBeHidden = (Button) findViewById(R.id.buttonToBeHidden);
    
            findViewById(R.id.openSecondActivity).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivityForResult(new Intent(MainActivity.this, SecondActivity.class), REQUEST_CODE_SECOND_ACTIVITY);
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_CODE_SECOND_ACTIVITY && resultCode == RESULT_OK) {
                //Check if you passed 'true' from the other activity to show the button, and also, only set visibility to VISIBLE if the view is not yet VISIBLE
                if (data.hasExtra(SHOW_BUTTON) && data.getBooleanExtra(SHOW_BUTTON, false) && mMyButtonToBeHidden.getVisibility() != View.VISIBLE) {
                    mMyButtonToBeHidden.setVisibility(View.VISIBLE);
                }
            }
        }
    }
    

    SecondActivity:

    public class SecondActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
    
            findViewById(R.id.hide_main_activity_button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra(MainActivity.SHOW_BUTTON, true);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题