在 Android Studio 中隐藏和取消隐藏布局中的堆栈

Stack in Hiding and unhiding layout in Android Studio

我正在努力使用我的应用程序。它应该像下面这样工作。

Every time I click the button it will hide the layout with an id of pic1 and if its already hidden it will unhide when I click the button again.

但是,问题是每次我点击它,它都会回到上一个Activity

这是我的布局中的 2 个布局和一个按钮。

<LinearLayout
    android:id="@+id/backpic"
    android:layout_width="350dp"
    android:layout_height="425dp"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:layout_marginTop="70dp"
    android:background="@drawable/back" />

<LinearLayout
    android:id="@+id/frontpic"
    android:layout_width="350dp"
    android:layout_height="425dp"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:layout_marginTop="70dp"
    android:background="@drawable/front" />

<Button
    android:id="@+id/btnRotate"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    android:layout_marginBottom="20dp"
    android:background="@drawable/rotate"
    android:onClick="rotate"/>

如果点击按钮,将执行函数rotate()

public class MusclesActivity extends AppCompatActivity {
    Button btnRotate;
    LinearLayout pic1,pic2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

         btnRotate = (Button)findViewById(R.id.btnRotate);
         pic1 = (LinearLayout)findViewById(R.id.frontpic);
         pic2 = (LinearLayout)findViewById(R.id.backpic);

        setContentView(R.layout.activity_muscles);
    }
}

旋转函数是

public void rotate(View views) {
    if (pic1.getVisibility()==View.VISIBLE)
        pic1.setVisibility(View.INVISIBLE);
    else
        pic1.setVisibility(View.VISIBLE);
}

我做错了什么?

尝试下面的代码一次,如果可行请告诉我。

更新

public class MusclesActivity extends AppCompatActivity {
Button btnRotate;
LinearLayout pic1,pic2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
 setContentView(R.layout.activity_muscles); // place it here


 btnRotate = (Button)findViewById(R.id.btnRotate);
 pic1 = (LinearLayout)findViewById(R.id.frontpic);
 pic2 = (LinearLayout)findViewById(R.id.backpic);
}