简体   繁体   中英

How to save background color from another acivity in android java

I want to let users choose background colors with animation in BackgroundActivity and saving that changed background color into MainActivity.

When a user clicks backgroundChange button on MainActivity, it moves to BackgroundActivity. Then there are a few different colors to choose. After a user click Save button after choosing color on BackgroundActivity, it is going back to MainAcitivity. My problem is I don't know how to save that changed background color from BackgroundAcivity to MainAcivity.

As a beginner, I cannot understand well how to use SharedPreferences. I checked several videos and searching many questions for hours about it, but still, I cannot figure out how to use SharedPreferences correctly on my own code.

BackgroundAcivity is really long that I will only put the first part. Could you tell me how to save this background change?

MainActivity

public class MainActivity extends AppCompatActivity {

    Button backgroundChange;

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

        backgroundChange = findViewById(R.id.backgroundChange);
        backgroundChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, BackgroundActivity.class);
                startActivity(intent);
            }
        });

    }
}

BackgroundActivity

public class BackgroundActivity extends AppCompatActivity {

    Button btn_blue, btn_purple, btn_orange, btn_save;
    View holderBg, dynamicBg;


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

        btn_save = findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BackgroundActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });


        btn_blue = findViewById(R.id.btn_blue);
        btn_purple = findViewById(R.id.btn_purple);
        btn_orange = findViewById(R.id.btn_orange);

        holderBg = findViewById(R.id.holderBg);
        dynamicBg = findViewById(R.id.dynamicBg);

        //set the first-time background
        holderBg.setBackgroundResource(R.drawable.bg_blue);
        holderBg.setScaleY(3);
        holderBg.setScaleX(3);

        //set the scale of button clicked
        btn_blue.setScaleY(1.5f);
        btn_blue.setScaleX(1.5f);

        btn_blue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //scale animation
                btn_blue.animate().translationY(20).scaleX(1.5f).scaleY(1.5f).setDuration(800).start();

                //default the scale buttons
                btn_purple.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();
                btn_orange.animate().translationY(0).scaleX(1).scaleY(1).setDuration(350).start();

                //change the background
                dynamicBg.animate().scaleX(3).scaleY(3).setDuration(800).start();
                dynamicBg.setBackgroundResource(R.drawable.bg_blue);

                //change color of button
                btn_save.setTextColor(Color.parseColor("#3498db"));


                //timer for change the holderbg
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        holderBg.setScaleX(3);
                        holderBg.setScaleY(3);
                        holderBg.setBackgroundResource(R.drawable.bg_blue);
                        dynamicBg.setScaleX(0);
                        dynamicBg.setScaleY(0);
                    }
                }, 850);
            }
        });
    }
}

You can use a bundle to transfer your data across Activity.

  • Saving data

Intent intent = new Intent(context, YourActivity.class);
intent.putExtra(KEY, "your value here");
startActivity(intent);

Then to retrieve data.

Intent intent = getIntent();
 if (intent != null ) { //Null Checking
    String strData= intent.getStringExtra(KEY);
    // do your work with `strData`
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM