By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,627 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,627 IT Pros & Developers. It's quick & easy.

Shared Preference Android Studio

P: 7
I had build an mobile app which consist of 2 activity as shown in figure below.It can let user to choose for go to main activity or the second activity

Main activity let user to send text to the second activity.
Second Activity let user to recieve text from main activity.

Can somebody teach me how i can save the text received in the second activity by using shared preference directly. That mean everytime second activity recieved text, it can save the text itself and user dont need to press an save button manually for saving that text

Below shown the layout of my app and code for that 2 activity


Expand|Select|Wrap|Line Numbers
  1. public class MainActivity extends Activity {
  2.  
  3.     EditText SendValue;
  4.     Button SendEditTextValue;
  5.     Intent intent;
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_main);
  10.  
  11.         SendEditTextValue = (Button)findViewById(R.id.button1);
  12.         SendValue = (EditText)findViewById(R.id.editText1);
  13.  
  14.         SendEditTextValue.setOnClickListener(new View.OnClickListener() {
  15.  
  16.             @Override
  17.             public void onClick(View v) {
  18.  
  19.                 intent = new Intent(getApplicationContext(),SecondActivity.class);
  20.                 intent.putExtra("EdiTtEXTvALUE", SendValue.getText().toString());
  21.                 startActivity(intent);
  22.  
  23.             }
  24.         });
  25.     }
  26. }


Expand|Select|Wrap|Line Numbers
  1. public class SecondActivity extends Activity {
  2.  
  3.     TextView receive;
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_second);
  8.  
  9.  
  10.  
  11.  
  12.  
  13.         receive = (TextView)findViewById(R.id.textView1);
  14.  
  15.         receive.setText(getIntent().getStringExtra("EdiTtEXTvALUE"));
  16.  
  17.  
  18.     }
  19.  
  20.  
  21. }
Attached Images
File Type: png App_layout.PNG (7.3 KB, 41 views)
File Type: png MainActivity_layout.PNG (6.0 KB, 21 views)
File Type: png SecondActivity_layout.PNG (7.3 KB, 23 views)
Mar 2 '18 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.