0

I just started learning developing android app using Android Studio, and I made a sample app that will store a value from two ExitText on one activity (firstActivity) and load these two values on the other activity (secondActivity) when I click the 'load' button on the secondActivity. However I couldn't load them on the secondActivity right from the firstActivity. Can someone help me with this.

Thanks in advance.

Edit:

Here is the scenario.

I have three activities say ActivityOne, ActivityTwo and ActivityThree

On ActivityOne I have EditText1, EditText2 and Button1 When I click Button1 data I entered into EditText1 and EditText2 should be saved. (Sharedpreferences)

On ActivityTwo I have another button called ButtonShow. When I click ButtonShow it should open ActivityThree with the values I stored previously from ActivityOne (EditText1 & EditText2).

Thank you very much for your help.

user3765415
  • 99
  • 1
  • 5
  • 17
  • edit ur questtion and post the code – KOTIOS Aug 15 '14 at 14:53
  • You have to pass the data to the new activity. http://stackoverflow.com/questions/19286970/using-intents-to-pass-data-between-activities-in-android look at the accepted answer – user1282637 Aug 15 '14 at 14:55
  • You can go with [this][1] way, about putextra stuff [1]: http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data – Ozan Aug 15 '14 at 14:55

2 Answers2

0

If you want to pass values between activities you can use SharedPreferences For example: Class A

String username2= "AAAA";
//to store the value use
SharedPreferences userDetails = A.this.getSharedPreferences("userdetails", MODE_PRIVATE);
                Editor edit = userDetails.edit();
                edit.clear();
                edit.putString("username", username2);
                //if you need to store more values you can add  here
                edit.commit();

Class B

//to get the value just do this
SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
            String  USERNAME = userDetails.getString("username", "");
            //if you need to get more value do it here
    //now you have your value username2 in USERNAME, now you can use it everywhere 
0

You should use the Bundle feature. You can put variables into this bundle, then attach it to an activity, and once that activity is started, you can get the variables you put into it, out of it again and then use them.

Here's an example (this code is called in FirstActivity, probably when clicking on a button):

Intent i = new Intent(getActivity(), SecondActivity.class);
Bundle variablesBundle = new Bundle();
Bundle.putString("EditText1Data", string1);
Bundle.putInt("EditText2Data", int1);
i.putExtras(variablesBundle);
startActivity(i);

And then this code is called in SecondActivity (perhaps in onCreate(), or wherever you want it)

Bundle bundle = getIntent().getExtras();
String myString = bundle.getString("EditText1Data");

That way you'll be able to pass data from 1 activity to another.

Hope the example helped clear it up a bit :)

Moonbloom
  • 7,738
  • 3
  • 26
  • 38