How to store lost used value in a variable and needs to use it when the application is started in android?
<blockquote>
  <p>How can we store a variable value even the application is closed.And
  needs to use that value when ever the application is opened .</p>
  
  <p>The below shown is my code.From the below code i'll get cityname and
  id of the city i need to use cityid in my next view.when ever i'm
  closing the application i'm losing id and name of the city.so for this
  i need to use last selected city as the default city for next time
  when ever the application is opened.</p>
</blockquote>

<pre><code>public class SampleActivity extends Activity {

Button back;
ListView lv;
CitiesParser parsedDataSet;
String url = "http://...........";
String pos;
String citypos;

ArrayList&lt;String&gt; cid;
MyArrayAdapter adapter;
// SaxParsing xparsing;
XMLParsing xparsing;

@SuppressWarnings({ "static-access" })
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    xparsing = new XMLParsing(url);

    ArrayList&lt;String&gt; cities = parsedDataSet.getdata();

    String[] scity = cities.toArray(new String[cities.size()]);

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

    adapter = new MyArrayAdapter(SampleActivity.this,
            android.R.layout.simple_list_item_single_choice, scity);

    lv.setAdapter(adapter);
    lv.setTextFilterEnabled(true);

    cid = parsedDataSet.getId();

    System.out
            .println("Hello from sample activity" + parsedDataSet.getId());

    System.out.println("before onclick in sampleactivity"
            + adapter.getItemId(0));

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2,
                long arg3) {
            Toast.makeText(SampleActivity.this,
                    "" + cid.get((int) adapter.getItemId(arg2)),
                    Toast.LENGTH_LONG).show();

            citypos = cid.get((int) adapter.getItemId(arg2));

        }
    });

    back = (Button) findViewById(R.id.backbutton);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Bundle b = new Bundle();
            b.putString("cityid", citypos);

            System.out.println("Main PutInt" + b);

            Intent in = new Intent(SampleActivity.this, Home.class);
            in.putExtras(b);
            startActivity(in);

        }
    });

    /*
     * public static void storeToPreference(Activity c,String value) {
     * SharedPreferences myPrefs = c .getSharedPreferences("cityname",
     * Activity.MODE_PRIVATE); SharedPreferences.Editor e = myPrefs.edit();
     * e.putString("yourkey", value); e.commit(); }
     */

}

private class MyArrayAdapter extends BaseAdapter {
    String[] items;

    public MyArrayAdapter(Context context, int textViewResourceId,
            String[] items) {
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.citieslist, null);

        }
        TextView tv = (TextView) v.findViewById(R.id.tvCity);
        tv.setText(items[position]);
        return v;
    }
}
</code></pre>

<p>}</p>
