使用 'onStart' 方法时如何声明 savedInstanceState
How to declare savedInstanceState when using 'onStart' method
是否可以在我的片段的 onStart
方法中使用 if (savedInstanceState == null) {...}
?每当我在 onCreate
方法中使用它时,我的片段都会显示为空白。
public class MyActivity extends AppCompatActivity {
private static final String TAG = MyActivity.class.getSimpleName();
private Boolean mCurrentValue;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate:::: retrieving preferences");
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mCurrentValue = mSharedPreferences.getBoolean("my_preference", false);
Log.d(TAG, "onCreate:::: my_preference and mCurrentValue=" + mCurrentValue);
if (mCurrentValue) {
setTheme(R.style.MyDarkAppCompatTheme);
Log.d(TAG, "onCreate:::: setTheme:MyDarkAppCompatNoActionBarTheme");
} else {
setTheme(R.style.MyLightTheme);
Log.d(TAG, "onCreate:::: setTheme:MyLightAppCompatNoActionBarTheme");
}
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
if (savedInstanceState == null) {
}
}
// in order to recreate Activity we must check value here because when we come back from another Activity onCreate doesn't called again
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.my_activity);
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Boolean mNewValue = mSharedPreferences.getBoolean("my_preference", false);
// if value differs from previous Theme, we recreate Activity
if(mCurrentValue != mNewValue){
recreate();
}
MyFragment newFragment = new MyFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
没有。如果您需要访问保存的数据片段,您需要从 onCreate 中的 savedInstanceState Bundle 中提取它们并将它们保存为 Activity 中的字段。如果您只需要检测此 Activity 是否被重新创建(通常检查 savedInstanceState 是否为空),您可以在 Activity 中声明一个布尔字段,并在 savedInstanceState 为 null 时在 onCreate 中设置它在范围内,然后在 onStart 中访问它:
private boolean activityRecreated;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityRecreated = savedInstanceState != null;
}
@Override
public void onStart() {
if (activityRecreated) {
// code here
}
}
编辑:在获得有关您尝试保存的状态的更多信息后,试试这个:
您应该在 Activity 中有一个字段来跟踪 ViewPager 的状态。此标签保存用户所在的选项卡。它可以是 String 标签或 Id (int) 标签。您应该通过覆盖 Activity:
中的 onSaveInstanceState 来保存它
private int tabIndex = 0 // NOTE: get this from your ViewPager's PagerAdapter
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tabIndex", tabIndex)
}
然后,在 onCreate 中,您可以检索此值:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
tabIndex = savedInstanceState.getInt("tabIndex", 0);
// here, make sure your ViewPager's PagerAdapter is created and set it to this tab
}
// rest of your onCreate goes here
}
以这种方式保存选项卡索引将允许 activity 重新创建正确的 ViewPager 选项卡可见。
是否可以在我的片段的 onStart
方法中使用 if (savedInstanceState == null) {...}
?每当我在 onCreate
方法中使用它时,我的片段都会显示为空白。
public class MyActivity extends AppCompatActivity {
private static final String TAG = MyActivity.class.getSimpleName();
private Boolean mCurrentValue;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate:::: retrieving preferences");
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
mCurrentValue = mSharedPreferences.getBoolean("my_preference", false);
Log.d(TAG, "onCreate:::: my_preference and mCurrentValue=" + mCurrentValue);
if (mCurrentValue) {
setTheme(R.style.MyDarkAppCompatTheme);
Log.d(TAG, "onCreate:::: setTheme:MyDarkAppCompatNoActionBarTheme");
} else {
setTheme(R.style.MyLightTheme);
Log.d(TAG, "onCreate:::: setTheme:MyLightAppCompatNoActionBarTheme");
}
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
if (savedInstanceState == null) {
}
}
// in order to recreate Activity we must check value here because when we come back from another Activity onCreate doesn't called again
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.my_activity);
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
Boolean mNewValue = mSharedPreferences.getBoolean("my_preference", false);
// if value differs from previous Theme, we recreate Activity
if(mCurrentValue != mNewValue){
recreate();
}
MyFragment newFragment = new MyFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
没有。如果您需要访问保存的数据片段,您需要从 onCreate 中的 savedInstanceState Bundle 中提取它们并将它们保存为 Activity 中的字段。如果您只需要检测此 Activity 是否被重新创建(通常检查 savedInstanceState 是否为空),您可以在 Activity 中声明一个布尔字段,并在 savedInstanceState 为 null 时在 onCreate 中设置它在范围内,然后在 onStart 中访问它:
private boolean activityRecreated;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityRecreated = savedInstanceState != null;
}
@Override
public void onStart() {
if (activityRecreated) {
// code here
}
}
编辑:在获得有关您尝试保存的状态的更多信息后,试试这个: 您应该在 Activity 中有一个字段来跟踪 ViewPager 的状态。此标签保存用户所在的选项卡。它可以是 String 标签或 Id (int) 标签。您应该通过覆盖 Activity:
中的 onSaveInstanceState 来保存它private int tabIndex = 0 // NOTE: get this from your ViewPager's PagerAdapter
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tabIndex", tabIndex)
}
然后,在 onCreate 中,您可以检索此值:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
tabIndex = savedInstanceState.getInt("tabIndex", 0);
// here, make sure your ViewPager's PagerAdapter is created and set it to this tab
}
// rest of your onCreate goes here
}
以这种方式保存选项卡索引将允许 activity 重新创建正确的 ViewPager 选项卡可见。