My database have two table, which is login and details.
login is used to store username (PK), and password and details is to store the username (FK), user's photo, details..
I want to keep the username shown in header so I can get the username and store it in details table.
How try to use bundles get extras() method to get the username and shown in header, but can't work if I intent back to those page.
Any idea how can I fix the username in header?
Or what should I do to ensure the user's details can store in the correct user database when user click "store button".
If you are calling Activity1 --- > Activity2
You can send the UserName by this method
Intent intent = new Intent(getBaseContext(), Activity2.class);
intent.putExtra("UserName ", UserName );
startActivity(intent);
To retrive the Extra() in Activity2, you need this code
String UserName = (String) getIntent().getSerializableExtra("UserName ");
Hope this helps
Below Edit for Better Understanding
public class Activity2 extends Activity {
private String UserName;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_2);
UserName= (String) getIntent().getSerializableExtra("UserName");
Log.i(Tag, "UserName: "+ UserName);
}
// you can call this method from click or where ever you want
private void AnyMethod()
{
Intent intent = new Intent(getBaseContext(), Activity3.class);
intent.putExtra("UserName ", UserName );
startActivity(intent);
}
}
You can have static object in the activity that object contains username and details of the user. When you intent back then you can assign current object to that static object. In this way you will be able to get the same user on previous activity.
Let's say you have an activity A from which you intent to the details activity. Create a public static user object that contains all the details of that user in A. When you intent back from details activity then assign A.user = current user object. In this way you can have the same user after you intent back and you can show username on the header on.
Related
Here I have 3 activity and ActivityA contain textView which show username , ActivityB contain only button to redirect ActivityC, ActivityC Contain EditText and save button.
Now i want to change updated text from ActivityC to direct ActivityA when save button clicked without refreshing any Activity than what i have to do? can you please suggest simple way.
I have a same requirement in complex project, i need to save user location at server side using api call and show updated data in ActivityA.
Another way would be to use LocalBroadcastManager.
The code will be as follows.
//ACTIVITY A CODE
TextView username_tv;
String username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
username_tv = (TextView) findViewById(R.id.username_tv);
//Get your username and store it in username
username = getYourUsername();
username_tv.setText(username);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
lbm.registerReceiver(receiver, new IntentFilter("USER_NAME_CHANGED_ACTION"));
}
public BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
username = intent.getStringExtra("username");
username_tv.setText(username);
}
}
};
//ACTIVITY C CODE
//ADD this code to your 'SAVE' Button Listener
Intent intent = new Intent("USER_NAME_CHANGED_ACTION");
intent.putExtra("username", editText.getText().toString());
// put your all data using put extra
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
For more information on localBroadcastManager look here.
I do not understand why you need this, but you can do it by saving the value of EditText in ActivityC in SparedPreferences here is an example how can make it. Then you should check in onResume() method of ActivityAif there is saved value in SparedPreferences and if has some get the value and set it to "Username" TextView.
EventBus will solve your problem
http://greenrobot.org/eventbus/
I'm developing an app where user must be able to switch between his accounts. As of now I'm able to allow the app with one account log in. How can I allow the user to create an another account and then switch between them in my android app?
update:-
I'm able to auth by Google button (firebase)
I want to do something like this in my app!
In my login.class I stored the value of UID which i got from firebase. Now i want to send this data to my splashscreen where it will check if uid==null then it will redirect to login and if uid !=null then it will redirect to MainActivity.
login.class
String MyPREFERENCES = "MyPrefs" ;
String uid = "uidKey";
SharedPreferences sharedpreferences = login.this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("uid", UID);
editor.putBoolean("is_logged_before",true); //this line will do trick
editor.commit();
Toast.makeText(login.this,"uid:"+UID,Toast.LENGTH_LONG).show();
String uid1 = sharedpreferences.getString("uid", "");
Log.i("shareduser",uid1);
Intent i = new Intent(login.this,splashScreen.class);
i.putExtra("message",uid1);
startActivity(i);
splashscreen
public class splashScreen extends Activity {
private static int SPLASH_TIME_OUT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/*Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
Log.i("received uid",message);*/
Intent homeIntent = new Intent(splashScreen.this, login.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
}
}
The scope of your answer is very wide. Please ask only genuine doubts related to programming only.
Looks like you need to implement Auto-Login of the User. For this you need to use Shared Preferences.
After successful login of the user, store the username and password in the shared preferences.
Then when the user again tries to login you need to check the sharedPref to check for the username and password.
Moreover it again depends on the type of authentication you are implementing as you can also get the Session Id for the user.
Check this answer for that
Also for Account Picker check this answer
I've an application where a user creates events. The user need to retrieve a certain name from an activity which is a ListView of names list.
I'm having an issue with making sure that a name should remain in an activity after clicking a date button which links to another activity(calendar activity), then return back to the current activity.
My codes of the 3 pages:
Create_Events.java - codes for getting a certain name from ListView activity and the btnDate onClickListener which links to the another activity(calendar activity)
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
String date = bundle.getString("date");
txtDate.setText(date);
}
Bundle b = getIntent().getExtras();
if(b != null)
{
String name = bundle.getString("name");
txtName.setText("Create an event for:" +name);
}
buttonDate = (Button) findViewById(R.id.btnDate);
buttonDate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent calIntent = new Intent(Create_Events.this, Calendar_Event.class);
startActivity(calIntent);
}
});
ContactsList.java -- the ListView of the names which is passed to the Create_Events page.
Cursor cursor = null;
cursor = (Cursor) l.getItemAtPosition(position);
Intent intent = new Intent(ContactsList.this, Create_Events.class);
intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
startActivity(intent);
I need help with this. Any help provided will be greatly appreciated. Thanks in advance! =)
you can get this behavior by saving you current screen state,
you can either use shared preferences or other ways (xml,data base, ..),
this way before you leave the activity (onPause) you save any information you need..
and on (onResume) if the information exists (its not the first time the activity loads),
collect the data and put it on screen..
if this is too much for you and you only need the name string to save,
try doing this :
How to declare global variables in Android?
hope it helps...
okay what i understand from your question is you want to retain your data on screen after coming back from another activity.
like Activity A--> Activity B--> Activity A
so, set in menifest file for activity A
android:launchmode="singletop"
and, when you are coming back from Activity B to Activity A
set
Intent intent=new Intent(ActivtyB.this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
you can use SharedPreferences to store the name while use bundle to store the date.
From contactLists.java add these codes
private void SavePreferences(String key, String value)
{
SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences()
{
SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String name = sharedPref.getString("name", "");
}
Then set the name to the textView which will show on the listView. And then load the preference in the create_events page and the name will be shown even when you go to another activity.
Do inform me if you still have any questions. (:
Im completely new to Android so apologies if this is a silly question. But my problem is this:
I have created a number of classes for each of the pages in my app and I would like to update the text in a textview of a particular class from the text in an edittext field from another class.
To be more specific, I have a login page and I want the username (input by the user in an edittext box) to be transferred to a textfield in the logged in page. Currently I am trying to achieve this by using a click listener for the log in button in the log in page:
public void sign_in_click(View view) {
EditText tv1 = (EditText) findViewById(R.id.txt_input_uname);
String username=tv1.getText().toString();
LoginDetails unamede=new LoginDetails();
unamede.setuname(username);
Intent myIntent = new Intent(view.getContext(), customer.class);
startActivityForResult(myIntent, 0);
}
So the click listener initialises a new class variable I have defined in another class like so:
public class LoginDetails {
public String uname;
public void setuname(String username){
uname=username;
}
public String getuname(){
return uname;
}
}
and it gives uname the username from the edittext box in the login page.
Then I have in the logged in page under oncreate:
LoginDetails unamed= LoginDetails();
String username=unamed.getuname();
tv1.setText(username);
but the text in the textview box doesnt get anything written to it. Now I wont be surprised if I'm doing this completely wrong but any advice would be much appreciated. thanks
What i would suggest is put the user's log in information into a SharedPreference. To transfer the date to another activity.
For example...
SharedPreferences myPrefs = getSharedPreferences("USER_INFO", 0);
myPrefsEdit.putString("USER_ID", username=tv1.getText().toString(););
//This is the username you get from edittext
myPrefsEdit.putString("PASSWORD", password);
//this is the user password you get from edittext
myPrefsEdit.commit();
In your next activity.
Get reference to your SharePreference like...
SharedPreferences info = getSharedPreferences("USER_INFO, 0);
String username = info.getString("USER_ID", "DOESNT EXIST");
String userpassword = info.getString("PASSWORD", "DOESNT EXIST");
This should do it
You could try putting the username to activity intent extra (when you don't want to persist it in the shared preferenced right now):
Intent myIntent = new Intent(view.getContext(), customer.class);
myIntent.putExtra("uname", username);
startActivityForResult(myIntent, 0);
And then in onCreate:
tv1.setText(getIntent().getStringExtra("uname"));
I have a login page; when I successfully login, the logged person's name should come on the next activity like so:
Welcome xyz
How can I do that?
Do something like this and always remember Phil's comment
In Login Activity
String userName;
if(login_ok){
final Intent i = new Intent(Login.this, Welcome.class);
i.putExtras("userName",userName);//sending checked value to next activity
startActivity(i);
}
in welcome Activity
TextView view1 = (TextView)findViewById(R.id.textView1);
Bundle bundle = getIntent().getExtras();
String user=bundle.getString("userName"));
view1.setText("Welcome "+user);