简体   繁体   中英

How to save the state of my activity after closing it.

Im new to android studio. I have an activity which is settings on turning on and off the music. When I turn of the music the image also changes to know that the music is turned off. but when i close the activity it goes back to the "playing music" image. How can I save the previous image or state of my activity after closing it. Do i need some save instance state something like that? Here is my code

package com.example.mainmenu;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;

import android.view.MotionEvent;
import android.view.View;

import android.view.WindowManager;
import android.widget.ImageView;


 public class settings extends AppCompatActivity {
 private ImageView close;
 private ImageView help;
 protected void onCreate(final Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,         
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custompopup);


    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    final ImageView sound;
    close =(ImageView) findViewById(R.id.txtclose);
    sound = (ImageView) findViewById(R.id.button_sound);
    help = (ImageView) findViewById(R.id.button_help);

    help.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent secondactivtyintent = new Intent(settings.this, 
    panuto1.class);
            startActivity(secondactivtyintent);

        }
    });
    help.setOnTouchListener(new View.OnTouchListener(){

        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction())
            {
                case MotionEvent.ACTION_DOWN :
                    help.setImageResource(R.drawable.help);
                    break;
                case MotionEvent.ACTION_UP :
                    help.setImageResource(R.drawable.help1);
                    break;
            }
            return false;
        }

    });
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            moveTaskToBack(false);
            finish();
        }
    });


    sound.setOnClickListener(new View.OnClickListener()
    {

        private boolean fun = true;
        public void onClick(View v)
        {
            if(fun)
            {
                sound.setImageResource(R.drawable.nosound);
                sound.setPressed(true);
                fun=false;
                stopService(new Intent(settings.this, 
   BackgroundSoundService.class));

                sound.setOnTouchListener(new View.OnTouchListener(){

                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction())
                        {
                            case MotionEvent.ACTION_DOWN :
                                sound.setImageResource(R.drawable.nosound1);
                                break;
                            case MotionEvent.ACTION_UP :
                                sound.setImageResource(R.drawable.nosound);
                                break;

                        }
                        return false;
                    }

                });

            }
            else
            {
                fun=true;
                sound.setPressed(true);
                sound.setImageResource(R.drawable.sound);
                startService(new Intent(settings.this, 
   BackgroundSoundService.class));
                sound.setOnTouchListener(new View.OnTouchListener(){

                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction())
                        {
                            case MotionEvent.ACTION_DOWN :
                                sound.setImageResource(R.drawable.sound1);
                                break;
                            case MotionEvent.ACTION_UP :
                                sound.setImageResource(R.drawable.sound);
                                break;
                        }
                        return false;

                    }

                });
            }

        }

    });

  }

 }

Use SharedPreference to save current state, update value in SharedPreference whenever you you change status. When you get back from other activity get the status from sharedPreference and change you image.you can do this task in onResume method.

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