0% found this document useful (0 votes)
8 views2 pages

Java Shared Preferences Example

Uploaded by

abhinandpcdance
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Java Shared Preferences Example

Uploaded by

abhinandpcdance
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

java

package [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link].*;
public class MainActivity extends AppCompatActivity {
EditText rollNo, name, branch;
SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
rollNo = findViewById([Link]);
name = findViewById([Link]);
branch = findViewById([Link]);
Button save = findViewById([Link]);
Button newData = findViewById([Link]);
pref = getSharedPreferences("MyPrefs", MODE_PRIVATE);
[Link](v -> {
String r = [Link]().toString();
String n = [Link]().toString();
String b = [Link]().toString();
if ([Link]()) [Link]("Required");
else if ([Link]()) [Link]("Required");
else if ([Link]()) [Link]("Required");
else {
[Link]().putString("rollno", r)
.putString("name", n)
.putString("branch", b)
.apply();
startActivity(new Intent(this, [Link])); } });
[Link](v -> {
[Link]().clear().apply();
[Link]("");
[Link]("");
[Link]("");
}); }}
view

package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class view extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_view);
SharedPreferences pref = getSharedPreferences("MyPrefs",
MODE_PRIVATE);
String data = "Roll No: " + [Link]("rollno", "N/A") +
"\nName: " + [Link]("name", "N/A") +
"\nBranch: " + [Link]("branch", "N/A");
((TextView) findViewById([Link])).setText(data);}}

You might also like