Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 900809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:27:03+00:00 2026-05-15T15:27:03+00:00

I’ve got an activity with a search field with a near current location checkbox

  • 0

I’ve got an activity with a search field with a “near current location” checkbox and a listview to show the results. I’m now in the life cycle of the activity, implementing the onSaveInstanceState and onRestoreInstanceState methods. When the activity is destroyed and I get back to it, the listview with the results has disappeared, but the text in the search box and the checkbox state are restored, while I haven’t saved anything in onSaveInstanceState. Why? What is saved “automatically” and what do I need to save in onSaveInstanceState?

public class Atable extends Activity {
    private EditText mSearch;
    private static final int ACTIVITY_EDIT=0;
    private Button mSearchButton;
    private TextView mNoresults;
    private TextView mytext;
    private ListView lv;
    private CheckBox checkBox;
    private LocationManager locationManager;    


    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);        

        setContentView(R.layout.main);

        lv= (ListView)findViewById(R.id.listview);

        mNoresults = (TextView) findViewById(R.id.noresults);  
        mNoresults.setVisibility(View.GONE);

        mSearchButton = (Button)this.findViewById(R.id.button);
        checkBox = (CheckBox) findViewById(R.id.local_check);        

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        final Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        mSearchButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mNoresults.setVisibility(View.GONE);
                mSearch = (EditText) findViewById(R.id.search);  
                String tmp_str = mSearch.getText().toString().replace(" ","+");           

                HttpClient httpclient = new DefaultHttpClient();    

                // Prepare a request object
                String url = "http://www.atable.org/getRestaurantByQuery/?query=" + tmp_str;              

                if (checkBox.isChecked()) {

                    //get location
                    String provider = locationManager.getBestProvider(criteria, true);                
                    Location location = locationManager.getLastKnownLocation(provider);

                    if (location!=null) {

                        String lat = String.valueOf(location.getLatitude());
                        String lng = String.valueOf(location.getLongitude());

                        url += "&lat="+lat+"&lng="+lng;                   
                    }
                }

                HttpGet httpget = new HttpGet(url); 

                // Execute the request
                HttpResponse response;
                try {
                    response = httpclient.execute(httpget);
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {

                        InputStream instream = entity.getContent();

                        Reader r = new InputStreamReader(instream);                      

                        Gson gson = new Gson();
                        final RestaurantList restaurantList = gson.fromJson(r, RestaurantList.class);

                        int nResults = restaurantList.getSize();

                        if (nResults>0) {

                            lv.setVisibility(View.VISIBLE);

                            lv.setAdapter( new ArrayAdapter<String>(Atable.this ,android.R.layout.simple_list_item_1,restaurantList.getRestaurantNames()));

                            lv.setOnItemClickListener(new OnItemClickListener() {

                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                    Intent intent = new Intent(Atable.this, RestaurantDescription.class);
                                    Restaurant tmp_resto = restaurantList.getRestaurant((int)id);

                                    String tmp_categories = tmp_resto.getCategories().get(0);
                                    for (int i=1; i<tmp_resto.getCategories().size(); i++) {
                                        tmp_categories+=", "+tmp_resto.getCategories().get(i);
                                    }

                                    String address = tmp_resto.getStreet()+", "+tmp_resto.getStreetNumber()+"\n"+tmp_resto.getCity()+
                                                    " "+tmp_resto.getPostalCode()+"\n"+tmp_resto.getCountry();

                                    intent.putExtra("name", tmp_resto.getName());
                                    intent.putExtra("address", address);                                
                                    intent.putExtra("rating", tmp_resto.getRating());
                                    intent.putExtra("price_range", tmp_resto.getPriceRange());
                                    intent.putExtra("categories", tmp_categories);
                                    intent.putExtra("latitude", tmp_resto.getLatitude());
                                    intent.putExtra("longitude", tmp_resto.getLongitude());
                                    startActivityForResult(intent, ACTIVITY_EDIT);
                                }
                            });

                        }

                        else {
                            lv.setVisibility(View.GONE);
                            mNoresults.setVisibility(View.VISIBLE);
                        }

                        //Closing the input stream will trigger connection release
                        instream.close();
                    }   


                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

    }

    //Start a location listener
    LocationListener onLocationChange=new LocationListener() {
        public void onLocationChanged(Location loc) {
            //sets and displays the lat/long when a location is provided
            /*String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();   
            mytext.setText(latlong);*/
        }

        public void onProviderDisabled(String provider) {
        // required for interface, not used
        }

        public void onProviderEnabled(String provider) {
        // required for interface, not used
        }

        public void onStatusChanged(String provider, int status,
        Bundle extras) {
        // required for interface, not used
        }
    };

    //pauses listener while app is inactive
    @Override
    public void onPause() {
        super.onPause();
        locationManager.removeUpdates(onLocationChange);
    }

    //reactivates listener when app is resumed
    @Override
    public void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,onLocationChange);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Activity is getting killed", Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {      
        Toast.makeText(this, "onRestoreInstanceState", Toast.LENGTH_LONG).show();
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {       
        Toast.makeText(this, "onSaveInstanceState", Toast.LENGTH_LONG).show();
        super.onSaveInstanceState(outState);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:27:04+00:00Added an answer on May 15, 2026 at 3:27 pm

    First, don’t perform network requests on your UI thread. You’ll make your UI unresponsive for the duration of the operation and risk triggering ANRs. Consider using AsyncTask to query your server.

    By default Activities save and restore their view hierarchy state. Each View‘s implementation is responsible for deciding what parts of its state should be saved and restored. ListView is a special case as usual.

    ListView does save state about current list position and item selection, but all of that is useless without an adapter. If you set an adapter before the saved view hierarchy state is restored in Activity#onRestoreInstanceState, ListView will attempt to re-sync its view state with the data.

    When onSaveInstanceState is called and you have result data available, you can write your saved search results into the Bundle you’re given.

    In onCreate, check to see if the supplied Bundle is not null and if your data is present within it from a previous query. If it is, read it back out. Use it to construct a new adapter and call setAdapter on your ListView. ListView should take it from there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.