Reputation: 147
I have been working on my app and come to the point where I need to understand the Activity lifecycle and I don't think I get it. What I have is an Activity with seven buttons on it. When a user presses a button it opens a listActivity where a user can choose an item in the list, the title gets sent back to the calling Activity, and the calling activity adds the title next to the text on the button. (The method it uses is in a switch statement that depends on what request code comes back to it - I think it's a little weak for my purposes.) But if the user leaves the activity and comes back to it, via the back button or any other button, none of the extra information is there when they return - all that remains are the seven buttons with their original text. I would like the information to stay - how do I go about that?
Here is the code from the calling Activity:
package com.dinner.live;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class PlanMenu extends Activity {
static final private int CHOOSE_MONDAY = 0;
static final private int CHOOSE_TUESDAY = 1;
static final private int CHOOSE_WEDNESDAY = 2;
static final private int CHOOSE_THURSDAY = 3;
static final private int CHOOSE_FRIDAY = 4;
static final private int CHOOSE_SATURDAY = 5;
static final private int CHOOSE_SUNDAY = 6;
private boolean ButtonPushed = false;
private NotesDbAdapter mDbHelper;
private MenuDbAdapter menuDbHelper;
private Long menuID;
private String mTitleText;
private String m;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.plan_menu);
Toast.makeText(this, "Choose a day to pick a meal for!", Toast.LENGTH_LONG).show();
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
menuDbHelper = new MenuDbAdapter(this);
menuDbHelper.open();
}
public void mButtonHandler(View target)
{
switch(target.getId())
{
case R.id.monday:
if(ButtonPushed == false)
{
// Create new intent object and tell it to call the ColorPicker class
Intent monday = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(monday, CHOOSE_MONDAY);
ButtonPushed = true;
break;
}
else
Toast.makeText(this, "That button has already been pushed." , Toast.LENGTH_LONG).show();
break;
case R.id.tuesday:
// Create new intent object and tell it to call the ColorPicker class
Intent tuesday = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(tuesday, CHOOSE_TUESDAY);
break;
case R.id.wednesday:
// Create new intent object and tell it to call the ColorPicker class
Intent wednesday = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(wednesday, CHOOSE_WEDNESDAY);
break;
case R.id.thursday:
// Create new intent object and tell it to call the ColorPicker class
Intent thursday = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(thursday, CHOOSE_THURSDAY);
break;
case R.id.friday:
// Create new intent object and tell it to call the ColorPicker class
Intent friday = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(friday, CHOOSE_FRIDAY);
break;
case R.id.saturday:
// Create new intent object and tell it to call the ColorPicker class
Intent saturday = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(saturday, CHOOSE_SATURDAY);
break;
case R.id.sunday:
// Create new intent object and tell it to call the ColorPicker class
Intent sunday = new Intent(this, PlanMenuList.class);
// Start ColorPicker as a new activity and wait for the result
startActivityForResult(sunday, CHOOSE_SUNDAY);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode == RESULT_OK){
switch(requestCode)
{
case CHOOSE_MONDAY:
menuID = intent.getExtras().getLong("newRowId");
menuDbHelper.fetchMenu(menuID);
populateFields();
//String label = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID);
Button tv = (Button)findViewById(R.id.monday);
m = mTitleText;
tv.setText("Monday" + " - " + mTitleText);
break;
case CHOOSE_TUESDAY:
String labelT = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID);
Button tvT = (Button)findViewById(R.id.tuesday);
tvT.setText("Tuesday" + " - " + labelT);
break;
case CHOOSE_WEDNESDAY:
String labelW = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID);
Button tvW = (Button)findViewById(R.id.wednesday);
tvW.setText("Wednesday" + " - " + labelW);
break;
case CHOOSE_THURSDAY:
String labelTh = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID);
Button tvTh = (Button)findViewById(R.id.thursday);
tvTh.setText("Thursday" + " - " + labelTh);
break;
case CHOOSE_FRIDAY:
String labelFriday = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID);
Button tvFriday = (Button)findViewById(R.id.friday);
tvFriday.setText("Friday" + " - " + labelFriday);
break;
case CHOOSE_SATURDAY:
String labelS = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID);
Button tvS = (Button)findViewById(R.id.saturday);
tvS.setText("Saturday" + " - " + labelS);
break;
case CHOOSE_SUNDAY:
String labelSa = intent.getExtras().getString("menuString");//intent.getExtras().getLong(MenuDbAdapter.KEY_ROWID);
Button tvSa = (Button)findViewById(R.id.sunday);
tvSa.setText("Sunday" + " - " + labelSa);
break;
}
}
}
private void populateFields() {
if (menuID != null) {
Cursor note = menuDbHelper.fetchMenu(menuID);
startManagingCursor(note);
mTitleText = (note.getString(
note.getColumnIndexOrThrow(MenuDbAdapter.KEY_TITLE)));
}
}
}
Upvotes: 1
Views: 3282
Reputation: 77762
Depends. In general, you'll want to override onSaveInstanceState() and save all the data into the Bundle (it's a simple key/value pair affair) and recreate that in onCreate() (or onRestoreInstanceState(), if you want to separate the code out).
However, this is mostly for those instances where the app is destroyed and recreated because the user changed the orientation.
If you want the data to be persistent (i.e. you go to that activity, return to the calling activity, possibly even switch to a different app altogether and then come back), I recommend saving everything into your SharedPreferences in onPause() and then put it back in place in onResume(). That way, the data will be in persistent storage and even outlive a system reset.
Upvotes: 1