Domanda

I've settings(called from onCreateOptionMenu) from my Activity which uses to update the UI on current Activity.

Starting Preferences on updating Preferences, Calling Activity needs to update UI on Preference basis.

Snippet how Preference called:-

Intent in = new Intent(this, PrefsSecondaryActivity.class);
in.putExtra("caller", "sx");
startActivityForResult(in, SECSETTINGS);

Catch to get the UI updates

if (requestCode == SECSETTINGS) {
Intent intent = getIntent();
finish();
startActivity(intent);
}

I used to Re-Create that activity with the above snippet. Inside of onCreate() of Activity. I checked the Preference Name-value Pair and update the UI which workd perfectly fine.

How to store the values which are inside that activity while destroying and recreating activity?

As I'm Destroying and Recreating activity which renders whole new Activity with no values inside of it.

I tried to set onSavedInstanceState() while calling Preferences and onRestoreInstanceState() is called in catch the onActivityResult()

Settings values in Preferences makes good change of SLOC. So it's not preferrable way right now.

Any suggestion would be welcome.

È stato utile?

Soluzione

This is how I do that (I have some static variables declared in my Activity):

@Override
protected final void onRestoreInstanceState(final Bundle inState)
{
    // Restore the saved variables.
    isChartShown = inState.getBoolean("chart", false);
    qIndex = inState.getInt("index");
    scores = inState.getIntArray("scores");
}
@Override
protected final void onSaveInstanceState(final Bundle outState)
{
    // Save the variables.
    outState.putBoolean("chart", isChartShown);
    outState.putInt("index", qIndex);
    outState.putIntArray("scores", scores);
}

This code works for me. I use it for saving some state variables used to maintain the values upon rotation.

[EDIT]

Otherwise, if you force the app finishing, then you'd go for Sharedpreferences:
just save your values before finishing and reload them in onCreate.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top