问题
Please guide in the following class what I should save in.
Please remember I am using only one string which is I am retriving from getextra method and there is nothing which I think I should store in the following overridden method.
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
Can you guide me what to store in onsave instance method and what not to?
package com.wozzon.activity;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.wozzon.json.JSONService;
import com.wozzon.model.SearchCriteria;
import com.wozzon.pl.UIFactory;
import com.wozzon.util.Util;
import com.wozzon.util.WLConstants;
public class HomeBrowse extends Activity implements OnItemClickListener{
private final static String TAG = HomeBrowse.class.getSimpleName();
private ArrayList<BrowseRow> model=new ArrayList<BrowseRow>();
private BrowseAdapter adapter;
private ListView list;
private TextView browseTitle;
private TextView changeLink;
private SearchCriteria sc =SearchCriteria.getInstance();
private JSONArray jsonArray;
private ProgressDialog m_ProgressDialog = null;
private Runnable progressRunnable;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
String errorMsg =Util.getExtraValue(getIntent().getExtras(), WLConstants.ERROR_MSG);
list =(ListView)findViewById(R.id.browse_cats);
if(errorMsg!= null){
UIFactory.displayAlert(new AlertDialog.Builder(HomeBrowse.this), "Status", errorMsg);
}
progressRunnable = new Runnable(){
@Override
public void run() {
try {
loadData();
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
}
runOnUiThread(returnRes);
}
};
m_ProgressDialog = ProgressDialog.show(HomeBrowse.this,
"Please wait...", "Loading ...", true);
Thread thread = new Thread(progressRunnable);
thread.start();
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
Util.handleError(HomeBrowse.this, HomeBrowse.class, e.getMessage());
//UIFactory.displayAlert(new AlertDialog.Builder(HomeBrowse.this), "Error Loading categories", e.getMessage());
}
}
private void loadData()throws Throwable{//loading object
jsonArray = JSONService.getJsonArray(getResources().getString(R.string.catJson));
}
private void fillResultRows(JSONArray jsonArray, BrowseAdapter adapter)throws Throwable{
BrowseRow br = null;
try {
for(int a=0; a<jsonArray.length(); a++){
JSONObject jsonObj = jsonArray.getJSONObject(a);
br = new BrowseRow();
br.name=jsonObj.getString("title");
br.image=jsonObj.getString("image");
br.searchType = jsonObj.getInt("searchType");
if(jsonObj.has("categoryIds")){
br.categoryIds = jsonObj.getString("categoryIds").replaceAll("-", ",");
}
if(jsonObj.has("subCategoryIds")){
br.subCategoryIds = jsonObj.getString("subCategoryIds").replaceAll("-", ",");
}
adapter.add(br);
}
} catch (Throwable e) {
throw e;
}
}
class BrowseAdapter extends ArrayAdapter<BrowseRow> {
BrowseAdapter() {
super(HomeBrowse.this, android.R.layout.simple_list_item_1, model);
}
public View getView(int position, View convertView,
ViewGroup parent) {
View row=convertView;
ResultWrapper wrapper=null;
if (row==null) {
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.browse_row, null);
wrapper=new ResultWrapper(row);
row.setTag(wrapper);
}
else {
wrapper=(ResultWrapper)row.getTag();
}
wrapper.populateFrom(model.get(position));
return(row);
}
}
class ResultWrapper {
private TextView name;
private String catIds="0";
private String subCatIds="0";
private ImageView image;
private int searchType;
private View row=null;
ResultWrapper(View row) {
this.row=row;
}
void populateFrom(BrowseRow r) {
getName().setText(r.name);
getIcon().setImageResource(getImageIcon(Integer.parseInt(r.image)));
catIds =r.categoryIds;
subCatIds = r.subCategoryIds;
searchType =r.searchType;
}
TextView getName() {
if (name==null) {
name=(TextView)row.findViewById(R.id.browse_row_name);
}
return name;
}
ImageView getIcon() {
if (image==null) {
image=(ImageView)row.findViewById(R.id.browse_row_icon);
}
return image;
}
}
private int getImageIcon(int catId){
int imageSource=R.drawable.all_cats;
switch(catId){
case WLConstants.CATEGORY_FILM: imageSource =R.drawable.film; break;
case WLConstants.CATEGORY_MUSIC: imageSource =R.drawable.music; break;
case WLConstants.CATEGORY_ARTS: imageSource =R.drawable.art; break;
case WLConstants.CATEGORY_KIDS: imageSource =R.drawable.museum; break;
case WLConstants.CATEGORY_GALLERY_MUSEUM: imageSource =R.drawable.kids; break;
case WLConstants.CATEGORY_COMEDY: imageSource =R.drawable.comedy; break;
case WLConstants.CATEGORY_NIGHT_CLUBS: imageSource =R.drawable.clubs; break;
case WLConstants.CATEGORY_ATTRACTION: imageSource =R.drawable.touristattractions; break;
case WLConstants.CATEGORY_VARIOUS_EVENTS: imageSource =R.drawable.all_cats; break;
case WLConstants.CATEGORY_ALL_FOOD_DRINK: imageSource =R.drawable.restaurants; break;
}
return imageSource;
}
@Override
public void onItemClick(AdapterView<?> adp, View view, int item, long arg3) {
try {
if("".equals(sc.getSearchLocation()) && (!(Util.locationFound(sc.getGeoLocation()) && sc.isUK()))){
UIFactory.displayAlert(new AlertDialog.Builder(HomeBrowse.this), "Error", "Please provide location");
return;
}
ResultWrapper wrap = (ResultWrapper)view.getTag();
sc.setCategoryIds(wrap.catIds);
sc.setSubCategoryIds(wrap.subCatIds);
sc.setSearchType(wrap.searchType);
goSearch();
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
Util.handleError(HomeBrowse.this, HomeBrowse.class, e.getMessage());
//UIFactory.displayAlert(new AlertDialog.Builder(this), "Error", e.getMessage());
}
}
private void applyListener(){
list.setOnItemClickListener(this);
changeLink.setOnClickListener(onclickListener);
}
private final void goSearch(){
try {
sc.setSearchString("");
Intent mainIntent = new Intent(HomeBrowse.this,SearchResults.class);
startActivity(mainIntent);
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
Util.handleError(HomeBrowse.this, HomeBrowse.class, e.getMessage());
}
}
public OnClickListener onclickListener = new OnClickListener(){
// @Override
public void onClick(View aView) {
try {
int componentId =aView.getId();
switch(componentId){
case R.id.tv_changeDateType:
Intent intent = new Intent(HomeBrowse.this,TimeLocationSettings.class);
startActivity(intent);
break;
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
Util.handleError(HomeBrowse.this, HomeBrowse.class, e.getMessage());
}
}
};
private void configComponents(){
String titleStr,location = "";
String dateCrtStr = Util.getDateCritieraStr(sc.getDateCriteria());
titleStr= dateCrtStr;
if(!"".equals(sc.getSearchLocation())){
location = sc.getSearchLocation();
}else if(sc.isNearMe()){
location="Near me";
}else{
location = "Postcode or town";
}
titleStr = titleStr + " - "+ location;
browseTitle.setText(titleStr);
changeLink.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
}
public class BrowseRow {
private String name = "";
private String image = "";
private String categoryIds="0";
private String subCategoryIds="0";
private int searchType=1;
}
private Runnable returnRes = new Runnable() {
@Override
public void run() {
try {
browseTitle = (TextView)findViewById(R.id.browsePageTite);
changeLink = (TextView)findViewById(R.id.tv_changeDateType);
changeLink.setText(Html.fromHtml("<a href='#'>Change</a>"));
adapter=new BrowseAdapter();
applyListener();
configComponents();
fillResultRows(jsonArray, adapter);
if(jsonArray == null){
UIFactory.displayAlert(new AlertDialog.Builder(HomeBrowse.this), "Error", "Error Loading categories");
return;
}
list.setAdapter(adapter);
} catch (Throwable e) {
e.printStackTrace();
}
m_ProgressDialog.dismiss();
}
};
}
回答1:
You should be saving everything that describes the view's state that isn't saved elsewhere.
Save where the user is in a EditText, what value the EditText has, what dialog is currently displayed, etc. Your application should be able to reconstruct the exact state it was in from the bundle returned to in when it is paused by the user leaving the application and returning to it.
回答2:
Not necessarily everything, because
The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)).
via http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
but if you override, you have to handle entire state by yourself.
来源:https://stackoverflow.com/questions/1954689/android-onsaveinstancestate-usage