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

时间:2018-01-20 10:20:06

标签: android

我是android studio新手。我有一项活动,可以打开和关闭音乐。当我关闭音乐时,图像也会变化,以便知道音乐已关闭。但当我关闭活动时,它会回到播放音乐"图片。关闭后,如何保存上一张图像或活动状态。我需要一些类似的保存实例状态吗?这是我的代码

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;

                    }

                });
            }

        }

    });

  }

 }

1 个答案:

答案 0 :(得分:0)

每当您更改状态时,使用SharedPreference保存当前状态,更新SharedPreference中的值。 当您从其他活动返回时,从sharedPreference获取状态并更改您的图像。您可以使用onResume方法执行此任务。