使用'onStart'方法时如何声明savedInstanceState

时间:2018-09-10 17:11:02

标签: java android android-activity android-preferences

是否可以在if (savedInstanceState == null) {...}方法中使用onStart来处理我的片段?每当我在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();
    }
}

1 个答案:

答案 0 :(得分:1)

不。如果需要访问已保存的数据,则需要从onCreate中的savedInstanceState捆绑包中提取数据并将其另存为Activity中的字段。如果您只需要检测是否重新创建了此Activity(与检查saveInstanceState是否为null相同),则可以在Activity中声明一个布尔字段,并在saveInstanceState在范围内时在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
    }
}

编辑:获得有关您要保存的状态的更多信息后,请尝试以下操作: 您的“活动”中应该有一个字段,该字段跟踪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
}

以这种方式保存选项卡索引将允许在可见的适当ViewPager选项卡的情况下重新创建活动。