1

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.

Mike
  • 4,550
  • 4
  • 33
  • 47
user3005486
  • 179
  • 1
  • 3
  • 9
  • How did you try to pass object with intent? Provide some code as well as logcat text – Anton Jan 22 '16 at 16:30
  • Intent returnBtn = new Intent(this, MainActivity.class); intent.putExtra("makeButtonVisible", true); startActivity(returnBtn); – user3005486 Jan 22 '16 at 16:38

3 Answers3

4

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();
            }
        });
    }
}
Mike
  • 4,550
  • 4
  • 33
  • 47
  • I already have one button in my Main Activity. I can not do 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); } }); – user3005486 Jan 22 '16 at 16:56
  • You question is pretty unclear, I just wanted to highlight the idea of how to communicate between 2 activities. Perhaps if you update your answer so I can have a more clear understanding on what you'd try to achieve, I can update the code to better fit your needs. – Mike Jan 22 '16 at 16:57
  • How do you open up the second Activity? – Mike Jan 22 '16 at 16:58
  • If you have some other button you just simply have to replace "R.id.openSecondActivity" with the ID of the View you open up the second Activity. – Mike Jan 22 '16 at 17:03
  • Ok. My main activity contains a button, wen pressed i will call a bluetooth class, created by me, to connect to a device. After connect to the device, i will call my second Activity. The second activity only shows a list with the connected devices. When I press de return button on the smartphone, i will return to the Main Activity, but this time I want to show a second button that will show me the list of devices to which I am already connected. – user3005486 Jan 22 '16 at 17:05
  • what I'm trying to do is put the second button visible once i enter the onCreate () in the second activity. I hope that i have explained well, not a english native. – user3005486 Jan 22 '16 at 17:12
2

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.

Mike
  • 4,550
  • 4
  • 33
  • 47
Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22
0

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();
} 
user3005486
  • 179
  • 1
  • 3
  • 9