activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity"
android:background="#e4e3de"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:textStyle="bold"
android:padding="15dp"
/>
<TextView
android:id="@+id/tv_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#2e729c"
android:textStyle="bold"
android:layout_below="@id/tv"
android:padding="15dp"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove Key City"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
res/values/strings.xml
<resources>
<string name="app_name">Android Example</string>
<string name="title_activity_settings">Settings</string>
<string name="title_activity_main">Android Example - Remove Key From SharedPreferences</string>
<!-- shared preferences keys-->
<string name="sp_key_country">country_name</string>
<string name="sp_key_city">city_name</string>
</resources>
MainActivity.java
package com.cfsuman.me.androidcodesnippets;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;
private RelativeLayout mRelativeLayout;
private TextView mTextView;
private TextView mTextViewSecond;
private Button mButton;
private SharedPreferences mSharedPreferences;
private SharedPreferences.Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the application context
mContext = getApplicationContext();
// Get the activity
mActivity = MainActivity.this;
/*
SharedPreferences
Interface for accessing and modifying preference data returned by
getSharedPreferences(String, int). For any particular set of preferences,
there is a single instance of this class that all clients share. Modifications
to the preferences must go through an SharedPreferences.Editor object to ensure
the preference values remain in a consistent state and control when they are
committed to storage. Objects that are returned from the various get methods must
be treated as immutable by the application.
*/
// Initialize the SharedPreferences object
mSharedPreferences = getPreferences(Context.MODE_PRIVATE);
/*
edit()
Create a new Editor for these preferences, through which you can make modifications
to the data in the preferences and atomically commit those changes back to the
SharedPreferences object.
*/
mEditor = mSharedPreferences.edit();
// Get the widgets reference from XML layout
mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
mTextView = (TextView) findViewById(R.id.tv);
mTextViewSecond = (TextView) findViewById(R.id.tv_second);
mButton = (Button) findViewById(R.id.btn);
// Put some values to shared preferences
mEditor.putString(getResources().getString(R.string.sp_key_country),"Bangladesh");
mEditor.putString(getResources().getString(R.string.sp_key_city), "Khulna");
mEditor.apply();
// Get the values from shared preferences
String country = mSharedPreferences.getString(getResources().getString(R.string.sp_key_country),"");
String city = mSharedPreferences.getString(getResources().getString(R.string.sp_key_city),"");
// Display the shared preferences values to TextView
mTextView.setText("SharedPreferences Values\n");
mTextView.setText(mTextView.getText() + "Country : " + country + "\nCity : " + city);
// Set a click listener for button widget
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
public abstract SharedPreferences.Editor remove (String key)
Mark in the editor that a preference value should be removed, which will be
done in the actual preferences once commit() is called.
Note that when committing back to the preferences, all removals are done first,
regardless of whether you called remove before or after put methods on this editor.
Parameters
key String: The name of the preference to remove.
Returns
SharedPreferences.Editor : Returns a reference to the same Editor object, so you can
chain put calls together.
*/
// Now remove the city value from shared preferences
mEditor.remove(getResources().getString(R.string.sp_key_city));
/*
public abstract boolean commit ()
Commit your preferences changes back from this Editor to the SharedPreferences
object it is editing. This atomically performs the requested modifications,
replacing whatever is currently in the SharedPreferences.
*/
//mEditor.commit();
/*
public abstract void apply ()
Commit your preferences changes back from this Editor to the SharedPreferences
object it is editing. This atomically performs the requested modifications,
replacing whatever is currently in the SharedPreferences.
*/
mEditor.apply();
// Again get the shared preferences values
String countryNow = mSharedPreferences.getString(getResources().getString(R.string.sp_key_country),"");
String cityNow = mSharedPreferences.getString(getResources().getString(R.string.sp_key_city),"");
// Again display the shared preferences values to TextView
mTextViewSecond.setText("SharedPreferences Values - After Removing City\n");
mTextViewSecond.setText(mTextViewSecond.getText() + "Country : " + countryNow + "\nCity : " + cityNow);
}
});
}
}

