Your experience on this site will be improved by allowing cookies
android prefrence
To store and retrieve the primitive information, i.e, the data of primitive data types such as string, integer, long, number, etc., the Android shared preference is used. The value can be retrieved on the basis of a key as the Android Shared Preferences store data in the key and value pair. To get the information from the user such as in settings, the use of Android Shared Preferences is preferred.
In the below example, we are demonstrating the usage of the android shared preference.
File: activity_main.xml:
File: DetailsActivity.java:
package com.example.radioapp; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class DetailsActivity extends AppCompatActivity { SharedPreferences prf; Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.details); TextView result = (TextView)findViewById(R.id.resultView); Button btnLogOut = (Button)findViewById(R.id.btnLogOut); prf = getSharedPreferences("user_details",MODE_PRIVATE); intent = new Intent(DetailsActivity.this,MainActivity.class); result.setText("Welcome "+prf.getString("username",null)); btnLogOut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = prf.edit(); editor.clear(); editor.commit(); startActivity(intent); } }); } } |
File: AndroidManifest.xml:
0 comments