0

So I want this button and TextView and LinearLayout to be invisible until a button is pressed on another activity, but when I return to MainActivity(the activity with the button and textview and linearlayout) the stuff is still invisible.

Thank you in advance.

MainActivity.java

textView.findViewById(R.id.textView);
textView.setVisibility(View.INVISIBLE);
ToggleButton button=findViewById(R.id.button);
button.setVisibility(View.INVISIBLE);
LinearLayout alarmLayout=findViewById(R.id.alarmLayout);
alarmLayout.setVisibility(View.INVISIBLE);
String value=getIntent().getStringExtra("buttonStatus");
    if(value.equals("Visible")){
        button.setVisibility(View.VISIBLE);
        alarmLayout.setVisibility(View.VISIBLE);
        textView.setVisibility(View.VISIBLE);

AlarmFrequency.java

Button create = findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent intent=new Intent(alarmFrequency.this,
        MainActivity.class);
        intent.putExtra("buttonStatus","Visible");

2 Answers2

0

Your if(value.equals("Visible")) is in onCreate? It should be under onResume otherwise it will only execute once when onCreate is called.

Android
  • 1,420
  • 4
  • 13
  • 23
lazy
  • 149
  • 8
0

I made little changes to your code to run it on my pc.

MainActivity.java

textView=findViewById(R.id.textView);
    textView.setVisibility(View.INVISIBLE);

    //make textView visible
    Intent i= getIntent();
    String value = i.getStringExtra("buttonStatus");
    if (i!=null && value!=null && value.equals("Visible")) {
        textView.setVisibility(View.VISIBLE);
    }

    //button to go to second activity
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(i);
        }
    });

SecondActivity.java

Button create = findViewById(R.id.create);
    create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SecondActivity.this, MainActivity.class);
            intent.putExtra("buttonStatus", "Visible");
            startActivity(intent);//launch main activity again 
        }
    });

This makes the textView visible in MainActivity.

But if you press back button to go back to MainActivity you will see no change.

When your application starts, the application stack has only MainActivity in it. Lets call it MainActivity1.

Then, you press a button to go to SecondActivity. Your application stack contents are now :- MainActivity1 / SecondActivity

Then when you launch MainActivity again from SecondActivity MainActivity2 get in the stack. Stack becomes MainActivity1 / SecondActivity / MainActivity2.

Changes are visible in MainActivity2, not in MainActivity1. If you use back button to go back to MainActivity1, textView will still be invisble.

Also note that, MainActivity1 was launched from another intent, and MainActivity2 was launched from different intent in SecondActivity.

Edit:-

If you want changes in your MainActivity1, you need to use startActivity for result. Following code changes will be required:-

MainActivity.java

textView=findViewById(R.id.textView);
    textView.setVisibility(View.INVISIBLE);

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, SecondActivity.class);
            startActivityForResult(i,100);
        }
    });

//outside onCreate

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(data!=null)
    {
        String value = data.getStringExtra("buttonStatus");
        if(value!=null && value.equals("Visible")) {
            Log.d("Debug", "i am here.");
            textView.setVisibility(View.VISIBLE);
        }
    }
}

SecondActivity.java

 Button create = findViewById(R.id.create);
    create.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SecondActivity.this, MainActivity.class);
            intent.putExtra("buttonStatus", "Visible");
            setResult(RESULT_OK, intent);
            finish();
        }
    });

Read more about this changes here how-to-pass-data-from-2nd-activity-to-1st-activity-when-pressed-back-android

Adarsh Anurag
  • 630
  • 5
  • 17