Passing data through Intent Android -
I am currently trying to program an app for which a user needs a key to their name. This is the "Login" page.
Enhances public class login activity {
EditText usernames; Override public null on @reate (bundle saved instances) {username = (EditText) findViewById (R.id.username); Super.onCreate (savedInstanceState); SetContentView (R.layout.login); Button Login button = (button) FindById (R.id.loginBtn); LoginButton.setOnClickListener (new View.OnClickListener) {public void onClick {intent = new intent ("com.example.MainActivity"); bundle extras = new bundle (); extras.putString ("name", Username.getText (). ToString ()); intent.putExtras (additional); initialization (intent);}}); } Secure void onDestroy () {super.onDestroy (); } Protected void (cause) {super.onPause (); SaveAsPreferences (); } Secure zero () {super. SaveAsPreferences (); } Secure zero () at reserve {super.onResume (); RetrievePreferences (); } Start over protected void {super.start (); RetrievePreferences (); } Secure void onStop () {super.onStop (); SaveAsPreferences (); } Public Zero Approval () {Shared Replacement Prefs = getSharedPreferences ("Preferences", MODE_PRIVATE); String name = username.getText (). ToString (); SharedPreferences.Editor Editor = prefs.edit (); Editor.putString ("name", name); Editor.commit (); } Recover public null () {shared references prefs = getSharedPreferences ("preferences", MODE_PRIVATE); If (prefs.contains ("name")) {string name = prefs.getString ("name", ""); Username.setText (name); }}
}
And this is my result page
Public class increases main activity activity {
@ override protected zero creation (bundle saved instainstate) {super.naught (savedinstenstate); SetContentView (R.layout.activity_main); Bundle bundle = getIntent (). GetExtras (); String name = bundle .getString ("name"); TextView Welcome User = (TextView) findViewById (R.id.welcomeUser); WelcomeUser.setText ("Hello" + name + "!");
}
Why are my codes not working? Thanks! Note: Before passing data I have linked the pages correctly.
You should change the order such as your login activity
in setContentView (R.layout.login); User name = (edit text) findViewById (R.id.username);
First you setContentView (..)
then reference view
.
Comments
Post a Comment