How to set a button visible from another activity

2020-05-06 16:49发布

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 problem setting the button visible, but when I return to the Main activity the button is still invisible.

This is my second Activity.

Button button = (Button) inflatedView.findViewById(R.id.showButton);
if (button.getVisibility() == View.INVISIBLE){
    button.setVisibility(View.VISIBLE);
}

This is the resume method in the MainActivity

@Override
protected void onResume() {
    super.onResume();
}

I already tried making the button visible in the Main Activity, and worked. But I want to make the button visible from the second class. I already tried passing an Intent from the second activity to the Main Activity but I don't know how to process the Intent in the Main Activity. I can not process the Intent in the onResume() or onCreate(), because it will throw a NullPointExeption.

Thanks for the help.

标签: android
3条回答
做自己的国王
2楼-- · 2020-05-06 17:19

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();
            }
        });
    }
}
查看更多
再贱就再见
3楼-- · 2020-05-06 17:20

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.

查看更多
走好不送
4楼-- · 2020-05-06 17:31

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();
} 
查看更多
登录 后发表回答