Saving view values while switching between activities in android - android

I'm working on a tabbed application in android. In the first tab I've a search engine with textviews, spinners.. and if I filled these params and switch to other tab or to the search results activity, and I return back to this tab (search engine tab) I found that all the fields are reinitialized. But I want to keep their value in order to remember what I searched for..
Is there a way to keep my data when I switch between activities ?
Thanks for your help.

You can override onSaveInstanceState (which will be called before you transition to a new activity) and onRestoreInstanceState (which will be called before your activity is resumed) e.g. :
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
super.onSaveInstanceState(savedInstanceState);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}

I guess you are creating intent object to initiate your tab's activity with addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP). Just remove this function if you have done something like this. For example , create intent object like below,
Intent intent = new Intent(this, tabActivity.class);
Not like this,
Intent intent = new Intent(this, tabActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
If you do so , your will get the exact state of a tab as u left it before switching to another tab.

Related

can we use Intent extras to preserve UI ?

I was reading about different methods used to preserve UI data across configuration changes and system-initiated process death. i came across this paragraph that i coundn't understanad.
can anyone explain?
Additionally, when you open an activity from an intent, the bundle of extras is delivered to the activity both when the configuration changes and when the system restores the activity. If a piece of UI state data, such as a search query, were passed in as an intent extra when the activity was launched, you could use the extras bundle instead of the onSaveInstanceState() bundle.
Link: https://developer.android.com/topic/libraries/architecture/saving-states
What they are saying is that when an Activity is restored after "configuration changes" or "system-initiated process death" you have multiple sources you can use to retrieve data from to get it setup as it was before e.g.:
Whatever was stored in the savedInstanceState bundle just before the Activity was killed.
Whatever was stored in the Intent that was used to start the Activity in the first place. This same Intent is still available after an activity is killed and
restored.
For example say you had a UserProfile Activity that showed information about the current user. When you launch the UserProfileActivity from some other activity, you could include information about the user in the Intent:
// Create Intent to start the `UserProfileActivity`
final Intent intent = new Intent(this, UserProfileActivity.class);
intent.putExtra("first_name", "John");
intent.putExtra("last_name", "Doe");
// Launch the intent
startActivity(intent);
Then in the UserProfileActivity you can retrieve that info:
// Get the intent that was used to launch this activity
final Intent = getIntent();
// Get the required info from the intent
final String firstName = intent.getStringExtra("first_name");
final String lastName = intent.getStringExtra("last_name");
// Update the UI
TextView tvName = findViewById(R.id.userName);
tvName.setText(firstName + " " + lastName);
Now say the the UserProfileActivity had an "About You" EditText field and a "Save" button which will store the contents of the EditText in their profile.
The user is part way through writing stuff in the "About You" EditText when they decide to rotate the screen. The activity gets killed but they haven't hit the "Save" button yet! All the stuff they wrote gets lost = unhappy user!
How can we create a better user experience? When the UserProfileActivity is about to get killed, onSaveInstanceState() will get call, so save the current contents of the "About You" EditText in the savedInstanceState bundle, and restore it once the activity is recreated.
#Override
protected void onSaveInstanceState(Bundle outState) {
// Store relevant state variables
final EditText etAboutYou = findViewById(R.id.aboutYou);
outState.putString("about_you", etAboutYou.getText().toString());
super.onSaveInstanceState(outState);
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
// Restore saved state if required
if (savedInstanceState != null) {
final String aboutYou = savedInstanceState.getString("about_you");
final EditText etAboutYou = findViewById(R.id.aboutYou);
etAboutYou.setText(aboutYou);
}
}

Have Activity A and B in activity

I Have Activity A and B. In activity A , I load data then when click on the list it move me to activity B. When go back to activity A the data load again. How to prevent this ?
When you move from one activity to another, say from activity A to activity B, then the data which you have loaded gets lost. To prevent the data reloading, you need to override onSaveInstanceState(Bundle savedInstance).
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
}
And you can extract those values by overriding onRestoreInstanceState(Bundle savedInstance).
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
Also you can store and retrieve the arraylist.

Android Save Instance State

I have created two activities A and B. In the Activity A, using onSaveInstanceState method I am saving bundle value ex(outState.putString("selectSaveDate", this.CalSelectedDate)) and going to the Activity B. When I hit back button to the Activity A , In the oncreate method the bundle value is null. I am unable to get my saved value in the oncreate method.
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.clear();
Log.i("bundleSave", "tester1" + this.CalSelectedDate);
outState.putString("selectSaveDate", this.CalSelectedDate);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null){
Log.i("todolist", "dsa" + savedInstanceState.getString("selectSaveDate"));
}
}
You only store data in a bundle in the OnSaveInstanceState method to persist data when your activity is destroyed and re-created (such as when rotating the screen or when the android os may decide to kill your activity if it is low on resources). When you launch activity B on top of your currently executing activity A, A is put in to a stopped state (therefore, your A activity is not destroyed). Also, when you come back from onStop, the next method that is called is onStart() (technically onRestart() is called be before onStart() but I find that callback is rarely ever implemented.
In conclusion, if your trying to keep persist data between launching an activity on top of your currently executing activity, you can just store that data in instance variables for that activity. If your trying to persist data between app launches then your going to want to look into storing data in Android's built in sqllite database or Android's SharedPreferences.
You should also obtain a real good understanding of the Activity lifecycle (its tricky but needed to code successfully in android):
http://developer.android.com/training/basics/activity-lifecycle/index.html
please try to Override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter like this:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "How are you");
// etc.
}
it will get passed in to onCreate and also onRestoreInstanceState where you'd extract the values like this:
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
or follow activity life cycle for better understanding.

save data while destroying and recreating activity

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.
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.

How to use onSavedInstanceState example please

I'm confused when it comes down to saving a state. So I know that onSaveInstanceState(Bundle) is called when the activity is about to be destroyed. But how do you store your information in it and bring it back to its original state in onCreate(Bundle savedInstanceState)? I don't understand how this bundle will restore information. It would be helpful if someone can provide an example.
The Dev guide doesn't do a good job of explaining this.
public class Conversation extends Activity {
private ProgressDialog progDialog;
int typeBar;
TextView text1;
EditText edit;
Button respond;
private String name;
private String textAtView;
private String savedName;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dorothydialog);
text1 = (TextView)findViewById(R.id.dialog);
edit = (EditText)findViewById(R.id.repsond);
respond = (Button)findViewById(R.id.button01);
if(savedInstanceState != null){
savedInstanceState.get(savedName);
text1.setText(savedName);
}
else{
text1.setText("Hello! What is your name?");
respond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = edit.getText().toString();
text1.setText("Nice to meet you "+ name);
}
});
}
}
#Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putString(savedName, name);
}
}
The Bundle is a container for all the information you want to save. You use the put* functions to insert data into it. Here's a short list (there are more) of put functions you can use to store data in the Bundle.
putString
putBoolean
putByte
putChar
putFloat
putLong
putShort
putParcelable (used for objects but they must implement Parcelable)
In your onCreate function, this Bundle is handed back to the program. The best way to check if the application is being reloaded, or started for the first time is:
if (savedInstanceState != null) {
// Then the application is being reloaded
}
To get the data back out, use the get* functions just like the put* functions. The data is stored as a name-value pair. This is like a hashmap. You provide a key and the value, then when you want the value back, you give the key and the function gets the value. Here's a short example.
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("message", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String message = savedInstanceState.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
Your saved message will be toasted to the screen.
One major note that all new Android developers should know is that any information in Widgets (TextView, Buttons, etc.) will be persisted automatically by Android as long as you assign an ID to them. So that means most of the UI state is taken care of without issue. Only when you need to store other data does this become an issue.
From Android Docs:
The only work required by you is to
provide a unique ID (with the
android:id attribute) for each widget
you want to save its state. If a
widget does not have an ID, then it
cannot save its state
A good information: you don't need to check whether the Bundle object is null into the onCreate() method. Use the onRestoreInstanceState() method, which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null
Store information:
static final String PLAYER_SCORE = "playerScore";
static final String PLAYER_LEVEL = "playerLevel";
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(PLAYER_SCORE, mCurrentScore);
savedInstanceState.putInt(PLAYER_LEVEL, mCurrentLevel);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
If you don't want to restore information in your onCreate-Method:
Here are the examples: Recreating an Activity
Instead of restoring the state during onCreate() you may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
mCurrentScore = savedInstanceState.getInt(PLAYER_SCORE);
mCurrentLevel = savedInstanceState.getInt(PLAYER_LEVEL);
}
Basically onSaveInstanceState(Bundle outBundle) will give you a bundle.
When you look at the Bundle class, you will see that you can put lots of different stuff inside it. At the next call of onCreate(), you just get that Bundle back as an argument.
Then you can read your values again and restore your activity.
Lets say you have an activity with an EditText. The user wrote some text inside it.
After that the system calls your onSaveInstanceState().
You read the text from the EditText and write it into the Bundle via Bundle.putString("edit_text_value", theValue).
Now onCreate is called. You check if the supplied bundle is not null. If thats the case,
you can restore your value via Bundle.getString("edit_text_value") and put it back into your EditText.
This is for extra information.
Imagine this scenario
ActivityA launch ActivityB.
ActivityB launch a new ActivityAPrime by
Intent intent = new Intent(getApplicationContext(), ActivityA.class);
startActivity(intent);
ActivityAPrime has no relationship with ActivityA.
In this case the Bundle in ActivityAPrime.onCreate() will be null.
If ActivityA and ActivityAPrime should be the same activity instead of different activities,
ActivityB should call finish() than using startActivity().
If Data Is not Loaded From savedInstanceState use following code.
The problem is url call is not to complete fully so, check if data is loaded then to show the instanceState value.
//suppose data is not Loaded to savedInstanceState at 1st swipe
if (savedInstanceState == null && !mAlreadyLoaded){
mAlreadyLoaded = true;
GetStoryData();//Url Call
} else {
if (listArray != null) { //Data Array From JsonArray(ListArray)
System.out.println("LocalData " + listArray);
view.findViewById(R.id.progressBar).setVisibility(View.GONE);
}else{
GetStoryData();//Url Call
}
}

Categories

Resources