简体   繁体   English

关闭活动后如何保存其状态。

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

Im new to android studio. 我是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. 使用SharedPreference保存当前状态,并在更改状态时更新SharedPreference值。 When you get back from other activity get the status from sharedPreference and change you image.you can do this task in onResume method. 当您从其他活动中回来时,请从sharedPreference获取状态并更改您的图片。您可以在onResume方法中执行此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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