0

The following code is trivial but simulates the problem.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnGoToExtra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="266dp"
        android:text="Go To Extra"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

layout_extra.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnBackToMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="186dp"
        android:text="Back To Main"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.btnGoToExtra)
                .setOnClickListener(v -> {
                    setContentView(R.layout.layout_extra);
                    findViewById(R.id.btnBackToMain)
                            .setOnClickListener(vv -> {
                                setContentView(R.layout.activity_main);
                            });
                });
    }
}

Question

I can navigate to the layout_extra from activity_main and return to activity_main. Unfortunately, after this round trip, I can no longer be able to navigate to layout_extra anymore.

What causes this problem and how to fix it?

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165

2 Answers2

2

Explanation will follow in chat but that is not how it is supposed to be done, For views to change, You have other options like using Fragments or using new activity entirely, I would also like to suggest moving to kotlin.

public class MainActivity extends AppCompatActivity {
    View mainView;
    View extraView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainView = getLayoutInflater().inflate(R.layout.activity_main, null);
        extraView = getLayoutInflater().inflate(R.layout.layout_extra, null);
        setContentView(mainView);

        mainView.findViewById(R.id.btnGoToExtra)
                .setOnClickListener(v -> {
                    setContentView(extraView);
                });
        extraView.findViewById(R.id.btnBackToMain)
                .setOnClickListener(vv -> {
                    setContentView(mainView);
                });
    }
}
asim
  • 533
  • 6
  • 17
2

Reading through the snippet above, here's how everything went

The line

findViewById(R.id.btnGoToExtra)
               .setOnClickListener

Registers Button with id btnGoToExtra for click events and when that happens you have the nested

findViewById(R.id.btnBackToMain)
                            .setOnClickListener

Which registers the element with id backToMain for click events and that's all

Calling setContentView() multiple times is not recommended. The normal way to switch between layouts in the same activity is to use a ViewFlipper or FrameLayout

From here

You then have:

XML

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView 
        android:src="@drawable/icon"
        android:scaleType="fitCenter"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"/>
    <TextView
        android:text="Learn-Android.com"
        android:textSize="24sp"
        android:textColor="#000000"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:gravity="center"/>
</FrameLayout>

code

private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}

private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}
Mab
  • 412
  • 3
  • 12