gpt4 book ai didi

java - 在Android上单击手机硬件后退按钮时从未调用过onResume?

转载 作者:行者123 更新时间:2023-12-01 16:55:03 24 4
gpt4 key购买 nike

我面临这个问题,我有MainActivity和SettingsPreference,所以当我从设置返回到MainActivity时,我想在MainActivity上重新加载适配器,我使用onResume(),当我单击ActionBar后退按钮时,它工作得很好,但是当我尝试时它位于硬件后退按钮上,它从未被调用,我发现系统调用 onPostResume() 而不是 onResume(),因此尝试使用它,但更改从未被应用

this is the SettingsActivity i'm using

当按下 ActionBar 后退按钮时,此代码起作用

      @Override
protected void onResume() {
super.onResume();
dataFromSharedPrefrence = getSettingsSharedPreferences();
adapterExample.notifyDataSetChanged();

}

按下硬件后退按钮时系统会调用 onPostResume(),但更改从未应用

@Override
protected void onPostResume() {
super.onPostResume();
dataFromSharedPrefrence = getSettingsSharedPreferences();
adapterExample.notifyDataSetChanged();
}

这是设置 Activity

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}


public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}

当我重写生命周期方法时,我发现了这种差异

  1. 按下 ActionBar 按钮时,这是输出
2020-05-05 06:00:44.899 23467-23467/com.example.test E/MainActivity: onDestroy
2020-05-05 06:00:44.907 23467-23467/com.example.test E/MainActivity: onDetachedFromWindow
2020-05-05 06:00:44.964 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:44.990 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.009 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.015 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.037 23467-23467/com.example.test E/MainActivity: onContentChanged
2020-05-05 06:00:45.048 23467-23467/com.example.test E/MainActivity: onStart
2020-05-05 06:00:45.051 23467-23467/com.example.test E/MainActivity: onPostCreate
2020-05-05 06:00:45.053 23467-23467/com.example.test E/MainActivity: onResume
2020-05-05 06:00:45.056 23467-23467/com.example.test E/MainActivity: onPostResume
2020-05-05 06:00:45.078 23467-23467/com.example.test E/MainActivity: onAttachedToWindow
2020-05-05 06:00:45.141 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.188 23467-23467/com.example.test E/MainActivity: onCreateView
2020-05-05 06:00:45.223 23467-23467/com.example.test E/MainActivity: onCreateView
  • 当按下硬件后退按钮时,这是输出

  • 2020-05-05 06:01:21.732 23467-23467/com.example.test E/MainActivity: onRestart
    2020-05-05 06:01:21.734 23467-23467/com.example.test E/MainActivity: onStart
    2020-05-05 06:01:21.738 23467-23467/com.example.test E/MainActivity: onResume
    2020-05-05 06:01:21.739 23467-23467/com.example.test E/MainActivity: onPostResume

    最佳答案

    试试这个:

    不要使用onResume(),而是尝试onStart():

      @Override
    protected void onStart() {
    super.onStart();
    dataFromSharedPrefrence = getSettingsSharedPreferences();
    adapterExample.notifyDataSetChanged();

    }

    关于java - 在Android上单击手机硬件后退按钮时从未调用过onResume?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61603140/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com