简体   繁体   English

如何不破坏 onBackPressed 中的活动?

[英]How not to destroy activity in onBackPressed?

I need that when calling the onBackPressed method, the activity is not destroyed but pauses and stops.我需要在调用onBackPressed方法时,活动不会被破坏,而是暂停和停止。

My idea is that it works like when the Home button of the mobile is pressed, which calls onPause and onStop because that way, the activity is not destroyed and when the activity is reopened, onResume is called so that the variables remain the value that when the activity was closed.我的想法是,它就像按下移动设备的 Home 按钮时一样工作,它调用onPauseonStop因为这样,活动不会被破坏,当活动重新打开时,会调用onResume以便变量保持当活动已结束。

Is there any way to do this?有没有办法做到这一点? If not, is there a way to keep the state of the variables once the activity is closed?如果没有,有没有办法在活动关闭后保留变量的 state? I have tried to use SharedPreference but I cannot use them because my variables are of different types than those offered.我曾尝试使用 SharedPreference 但我无法使用它们,因为我的变量与提供的变量类型不同。

If you want to customize the behaviour of your buttons, you have to use in your activity...如果你想自定义按钮的行为,你必须在你的活动中使用......

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        
        return true;    //To assess you have handled the event.
    }
    //If you want the normal behaviour to go on after your code.
    return super.onKeyDown(keyCode, event);
}

Here is some more information about handling key event.这是有关处理关键事件的 更多信息


Although it seems what you want to do is just retain the state of your activity.虽然看起来您想要做的只是保留您活动的 state。 The best way is to store your data before quitting and call it back when you recreate your activity.最好的方法是在退出之前存储您的数据,并在您重新创建活动时调用它。

If you want to store temporary data (I mean not save it between 2 boots), one way to do it would be to use sharedPreferences .如果您想存储临时数据(我的意思是不要在两次引导之间保存),一种方法是使用sharedPreferences

//Before your activity closes
private val PREFS_NAME = "kotlincodes"
private val SAVE_VALUE = "valueToSave"
val sharedPref: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
val editor: SharedPreferences.Editor = sharedPref.edit()
editor.putInt(SAVE_VALUE, value)
editor.commit()

//When you reopen your activity
private val PREFS_NAME = "kotlincodes"
val sharedPref: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
sharedPref.getString(SAVE_VALUE, null)

Another way to do it since you cannot use sharedPreferences (because you don't use primitive types), is to use a global singleton.由于您不能使用 sharedPreferences(因为您不使用原始类型),另一种方法是使用全局 singleton。

Here is a Java implementation...这是一个 Java 实现......

public class StorageClass{
    private Object data1 = null;
    private StorageClass instance;

    private StorageClass(){};

    public static final StorageClass getInstance(){
        if(instance == null){
            synchronized(StorageClass.class){
                if(instance==null)    instance = new StorageClass();
            }
        }
        return instance;
    }

    public void setData1(Object newData) { data1 = newData; }

    public Object getData1() { return data1; }
}

Then just use...然后就用...

StorageClass.getInstance().setData1(someValue);

... and... ... 和...

StorageClass.getInstance().getData1(someValue);

In Kotlin...在 Kotlin...

object Singleton{
    var data1
}

Which you use with...你用哪个...

Singleton.data1 = someValue    //Before closing.
someValue = Singleton.data1    //When restarting the activity.

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

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