SharedPreferences save a button change invisible after click on it and let appears another button

我怕爱的太早我们不能终老 提交于 2019-12-06 13:13:06

I made an example app and tested it. It saves the last state of textviews(visible or invisible) no matter on which activity you make changes. And even when you exit and come back to the app it loads the last state.

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.key.hs.invisiblebuttons">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".Activity1"
            android:label="Test1"
            android:alwaysRetainTaskState="true"
            android:configChanges="keyboardHidden|orientation|screenSize"
            >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
            <category android:name="android.intent.category.HOME"></category>
            <category android:name="android.intent.category.LAUNCHER"></category>

        </intent-filter>
        </activity>

        <activity
            android:name=".Activity2"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:hardwareAccelerated="true">
        </activity>
    </application>
</manifest>

Activity1.class

package com.key.hs.invisiblebuttons;

    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    /**
     * Created by Hasan on 26.04.2017.
     */

    public class  Activity1 extends AppCompatActivity {

        private boolean btn1visiblity, btn2visibility;
        private TextView tv1, tv2;
        private Button btn1, btn2, act2, reset;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity1lay);

            tv1= (TextView)findViewById(R.id.tv1);
            tv2=(TextView)findViewById(R.id.tv2);
            btn1= (Button)findViewById(R.id.btn1);
            btn2=(Button)findViewById(R.id.btn2);
            act2=(Button)findViewById(R.id.act2);
            reset=(Button)findViewById(R.id.reset);


            //Create or load preferences
            final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
            btn1visiblity = prefs.getBoolean("TV1visibility", true);
            btn2visibility = prefs.getBoolean("TV2visibility", true);

            //take into effect saved booleans
            if(btn1visiblity){
                tv1.setVisibility(View.VISIBLE);
            }else{
                tv1.setVisibility(View.INVISIBLE);
            }
            if(btn2visibility){
                tv2.setVisibility(View.VISIBLE);
            }else{
                tv2.setVisibility(View.INVISIBLE);
            }


            reset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn1visiblity = prefs.getBoolean("TV1visibility", true);
                    btn2visibility = prefs.getBoolean("TV2visibility", true);

                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV1visibility", true);
                    editor.putBoolean("TV2visibility", true);
                    editor.commit();

                    tv1.setVisibility(View.VISIBLE);
                    tv2.setVisibility(View.VISIBLE);

                }
            });

            act2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Activity1.this, Activity2.class);
                    startActivity(intent);
                    finish();

                }
            });

            btn1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Create or load preferences
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn1visiblity = prefs.getBoolean("TV1visibility", true);

                    //save new boolean
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV1visibility", false);
                    editor.commit();
                    tv1.setVisibility(View.INVISIBLE);
                }
            });

            btn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Create or load preferences
                    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                    btn2visibility = prefs.getBoolean("TV2visibility", true);

                    //save new boolean
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean("TV2visibility", false);
                    editor.commit();
                    tv2.setVisibility(View.INVISIBLE);
                }
            });

    }
    }

Activity2.class

     package com.key.hs.invisiblebuttons;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by Hasan on 26.04.2017.
 */

public class  Activity2 extends AppCompatActivity {

    private boolean btn1visiblity, btn2visibility;
    private TextView tv1, tv2;
    private Button btn1, btn2, act1, reset;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2lay);

        tv1= (TextView)findViewById(R.id.tv1);
        tv2=(TextView)findViewById(R.id.tv2);
        btn1= (Button)findViewById(R.id.btn1);
        btn2=(Button)findViewById(R.id.btn2);
        act1=(Button)findViewById(R.id.act1);
        reset=(Button)findViewById(R.id.reset);

        //Create or load preferences
        final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
        btn1visiblity = prefs.getBoolean("TV1visibility", true);
        btn2visibility = prefs.getBoolean("TV2visibility", true);

        //if tvs invisible in activity1 make them visible in activity2
        if(!btn1visiblity){
            tv1.setVisibility(View.VISIBLE);
        }else{
            tv1.setVisibility(View.INVISIBLE);
        }
        if(!btn2visibility){
            tv2.setVisibility(View.VISIBLE);
        }else{
            tv2.setVisibility(View.INVISIBLE);
        }

        //resets booleans
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn1visiblity = prefs.getBoolean("TV1visibility", true);
                btn2visibility = prefs.getBoolean("TV2visibility", true);

                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV1visibility", true);
                editor.putBoolean("TV2visibility", true);
                editor.commit();
                tv1.setVisibility(View.INVISIBLE);
                tv2.setVisibility(View.INVISIBLE);

            }
        });


        //go to activity1
        act1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity2.this, Activity1.class);
                startActivity(intent);
                finish();

            }
        });

        //makes tv1 invible
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create or load preferences
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn1visiblity = prefs.getBoolean("TV1visibility", true);

                //save new boolean
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV1visibility", true);
                editor.commit();
                tv1.setVisibility(View.INVISIBLE);
            }
        });

        //makes tv2 invisible
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create or load preferences
                final SharedPreferences prefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
                btn2visibility = prefs.getBoolean("TV2visibility", true);

                //save new boolean
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("TV2visibility", true);
                editor.commit();
                tv2.setVisibility(View.INVISIBLE);
            }
        });



    }
}

activity1lay.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"
        android:text="example tv1"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv2"
        android:text="example tv2"
        android:layout_alignParentEnd="true"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn2"
        android:text="TV2 invisible"
        android:layout_alignParentBottom="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn1"
        android:text="TV1 invisible"
        android:layout_above="@id/btn2"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/act2"
        android:text="ACTIVITY2"
        android:layout_alignParentEnd="true"
        android:layout_centerInParent="true"></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/reset"
        android:text="RESET VISIBILITY"
        android:layout_alignParentStart="true"
        android:layout_centerInParent="true"/>

</RelativeLayout>

activity2lay.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv1"
            android:text="example tv1"
            android:layout_centerInParent="true"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv2"
            android:text="example tv2"
            android:layout_above="@id/tv1"
            android:layout_centerHorizontal="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn1"
            android:text="TV1 invisible"
            android:layout_above="@id/btn2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:text="TV2 invisible"
            android:layout_alignParentBottom="true"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/act1"
            android:text="ACTIVITY1"
            android:layout_alignParentEnd="true"
            android:layout_centerInParent="true"></Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/reset"
            android:text="RESET VISIBILITY"
            android:layout_alignParentStart="true"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!