SharedPreferences in Android?

0

SharedPreferences in Android?

It is used to provide a persistent object that can be used in another activity, shared preferences store data using key=>value pair.

The key is used to provide a unique identity and value is used to store data. it is also called the state management object of the Android application to persistent the state of data from one activity to another.

........................................................................................................................................................

Syntax to Store Data Under Shared Preferences:-

 SharedPreferences sharedpreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
 SharedPreferences.Editor objectname = sharedpreferences.edit();
objectname.putInt("key",value);
objectname.putString("key",value);
editor.commit();

Syntax to Get Data from Shared Preferences:-

int var =    objectname.getInt("key",0);
String var = objectname.getString("key",null);

Example of Shared Preferences:-


package com.example.webviewexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SharedPreferencesExample extends AppCompatActivity {

Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences_example);
final SharedPreferences sharedpreferences =
getSharedPreferences("pref", Context.MODE_PRIVATE);
btn = findViewById(R.id.shbtnclick);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("rno", 1001);
editor.putString("sname","xyz");
editor.commit();
Intent i = new Intent(getApplicationContext(),MainActivity7.class);
startActivity(i);

}
});
}
}



MainActivity7.Java:-



package com.example.webviewexample;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity7 extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main7);
TextView txt = findViewById(R.id.txtactivity2);
@SuppressLint("WrongConstant") SharedPreferences sharedpreferences =
getSharedPreferences("pref",Context.MODE_APPEND);
int r = sharedpreferences.getInt("rno",0);
String s = sharedpreferences.getString("sname",null);
txt.setText("rno is "+r+ "name is "+s);


}
}


Design File of XML:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity7">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtactivity2"></TextView>
</LinearLayout>


                                






Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)