1

I have an Activity that uses a ViewSwitcher to switch between two views. The onCreate() method uses setContentview() to set the view to one of the two views.

When leaving the Activity and going back to it again later, I want the view that was shown last to be the one that is picked in the viewSwitcher (not the one in SetContentview every single time).

How would I accomplish this?

I tried something like this, but the app crashes when I try to load the Activity:

private View lastState = findViewById(R.id.activity_login_page);

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

    setContentView(lastState);
Jerry
  • 966
  • 2
  • 13
  • 28

1 Answers1

0

I achieved something similar by using ViewFlipper.

SettingsActivity

public class SettingsActivity extends AppCompatActivity {

   boolean state;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_settings);
       SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
       SharedPreferences.Editor editor = sharedPreferences.edit();

       Button right = findViewById(R.id.right);
       Button left = findViewById(R.id.left);
       right.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               state = true;
               editor.putBoolean("handstate", state);
               editor.apply();
           }
       });
       left.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               state = false;
               editor.putBoolean("handstate", state);
               editor.apply();
           }
       });
   }

ViewFlippingActivity

public class VIewFlippingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_flipping);
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);


        ViewFlipper flipper = findViewById(R.id.flipper);
        RelativeLayout layoutRight = findViewById(R.id.layoutRight);
        RelativeLayout layoutLeft = findViewById(R.id.layoutLeft);

        boolean statereceive = sharedPreferences.getBoolean("handstate", false);
        if (statereceive == true) {
            flipper.setDisplayedChild(flipper.indexOfChild(findViewById(R.id.layoutRight)));
        } else if (statereceive == false){
            flipper.setDisplayedChild(flipper.indexOfChild(findViewById(R.id.layoutLeft)));
        } else {
            flipper.setDisplayedChild(flipper.indexOfChild(findViewById(R.id.layoutRight)));
        }
    }

viewflipping_activity.xml

<ViewFlipper 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:id="@+id/flipper"
    android:layout_height="match_parent"
    tools:context=".VIewFlippingActivity">

    <RelativeLayout
        android:id="@+id/layoutRight"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:background="@color/royal_android">

          //your content
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layoutLeft"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:background="@color/royal_blue">
              //your content

    </RelativeLayout>


</ViewFlipper>

Sorry for the bad Explanation and difficult to read code im in a hurry. Hope it still provides some sort of help.