Simple Example using Shared Preferences between Activities

Do not let the Preference in the class name fool you. Yes the SharedPreference class can set preferences but it also a great way to implement an easy limited database. It is also a great way keep variables values after restarting the app.

At a high level this first activity (MainActivity) is getting values from EditText fields. When the button is clicked, is calls getResults(View v). This method grabs all the values from the EditText fields and adds them to SharedPreferences then calls “Results” activity. If the values have already been set then it sets the EditText fields to those values. I trimmed the code so this is not a complete app, but it will run.

 

MainActivity

package com.nathanhaze.gasmilage;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText mpg;
	private EditText miles;
	private EditText price;
	private EditText psi;
	private TextView title;
	private TextView subtitle;
	public int mpgvalue;
	public int milesvalue;
	public float pricevalue;
	public int psivalue;
	SharedPreferences shared;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		shared = getSharedPreferences("A", Context.MODE_PRIVATE); // get the set of Preferences labeled "A"

		mpg = (EditText)findViewById(R.id.avgmpg);
		miles = (EditText)findViewById(R.id.miles);
		price = (EditText)findViewById(R.id.gasprice);
		title = (TextView)findViewById(R.id.ResultTitle);
		subtitle = (TextView)findViewById(R.id.subtitle);
		psi = (EditText)findViewById(R.id.psivalue);

		if(shared.contains("mpg")){
    		mpg.setText(String.valueOf(shared.getInt("mpg", 0)));  // get the value with the key "mpg" but return 0 if issue
		}
		if(shared.contains("miles")){
			miles.setText(String.valueOf(shared.getInt("miles", 0)));
			}
		if(shared.contains("price")){
			price.setText(String.valueOf(shared.getFloat("price", 0)));
			}
		if(shared.contains("psi")){
			psi.setText(String.valueOf(shared.getInt("psi", 0)));
			}

	}

	public void getResults(View view){
		mpgvalue = Integer.valueOf(mpg.getText().toString()); // getting values from EditText field
		milesvalue = Integer.valueOf(miles.getText().toString());
		pricevalue = Float.valueOf(price.getText().toString());
		psivalue = Integer.valueOf(psi.getText().toString());

		final Editor editor = shared.edit();

		editor.putInt("mpg", mpgvalue);  // set values
		editor.putInt("miles", milesvalue);
		editor.putFloat("price", pricevalue);
		editor.putInt("psi", psivalue);

		editor.commit(); // commit it when you are done.

		Intent intent = new Intent(this, Results.class);
	        startActivity(intent);

	}
}

Results Activity

package com.nathanhaze.gasmilage;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.app.Activity;

public class Results extends Activity {

        SharedPreferences shared;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.results);

		shared = getSharedPreferences("A", Context.MODE_PRIVATE);   // get the sharedpreference set named "A"

                 float psi = shared.getInt("psi", 0);  // get value from key but return 0 if nothing is set

	}

}

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys