How to get an EditText field prefilled after hitting the back button - android

I have a setup view where the user can enter their name and email and click done when they are finished which navigates them to another activity1. When they are in activity1, and they hit the soft back button on the phone, it takes them screen where they entered their name and email. However, the name and email EditText fields are blank.
In the view where the name and email are entered I looked to see if onStart, onCreate, OnPause, onResume,... where being called after the back button was hit, but they are not.
Do you know how I can make it so that the EditText fields have the information that was previously entered.

Well, you could store the data in a singleton class, and when they go back to the activity, it checks if those values exist and populate the EditText.
Here's how you can override the onresume:
#Override
public void onResume() {
super.onResume();
// do whatever you need to do here
}

Related

Call a method when back button pressed

I have a list in my app. In another activity, user deletes a row from database and it returns to the main activity (I use finish() here). When it returns back, the row that has been removed from database is still on the list. If user presses refresh button, it disappears.
How can I refresh it automatic?
Override your original Activity's (the one that is showed when the user presses back) onResume(), so it looks like:
#Override
protected void onResume()
{
refreshListMethod();
super.onResume();
}
refreshListMethod(); is whatever approach you chose to refresh the List since you say:
user presses refresh button, it disappears.
So turn that approach into a method so you can use it wherever needed.
Recreate the Activity When you click a list item, you can call finish() on the ListActivity then recreate the Activity with an Intent after you delete the row in the new Activity

dialog activity control

What I want to do: When the application starts it reads an email address from a database. If the email address is NOT set, it starts another dialog activity for adding the email address.
If the user doesn't add the email, he cannot close that dialog activity, so clicking on close button doesn't close the dialog activity.
The problem that I have is that if the user clicks the back button, the dialog activity closes and the main activity starts without email address set. What I want to do is if the user clicks the back button in this case, to close the application.
I have to mention that I'm using the same dialog activity when I want to edit the email address.
Override onBackPressed, there check whether user has inserted any email address. If not just return without calling super.onBackPressed().
#Override
public void onBackPressed() {
if(yourEmailEditText.getText() == null
|| yourEmailEditText.getText().toString().trim().length() == 0){
return
}
super.onBackPressed();
}
Try Overriding the onBackPressed() method and do nothing into it.
You can set the Dialog as non-cancellable in one of the constructors second parameters:
Dialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)
Also take into the account what happens if the user by-pass the dialog by any means (any button by hardware, etc). You could simply interprete this as the user wants to exit the application.

Advice needed with back button on the device

I am working on an android app,coming to the point, one activity in the app has two edit text fields, the first field gets active that is gets enabled when the activity is started, and it is only after the first text field is filled, the second field becomes active. This part work correctly.
Now what the problem is when the user fills the second text field and presses the back button on the device, the second text field should get saved and should become inactive, also when the user again goes to the same activity, the second text field should remain inactive,and should get active that is editable only after the user double taps the second edit text field.
I know that database would be needed for saving the data entered in the second field,but even if that is used how should I resolve the problem above, I'd really appreciate your help.Thanks.
over ride this function onBackPressed(). In this function, write your code to "save" the text box contents and onResume(), you can re-load the text with the saved value.
override
onPause()
and
onResume()
to do your task.
You could aslo try startActivityForResult()

Android: User Input into a Text Field WebPage

I want the user to input these fields in a editText(s) and then login to the site using these fields which were previously entered (in a different activity). Also, how do I automatically click login without the user having to click it. By this, I mean in one activity, enter the login credentials and auto-login to the website in the background without the user needing to see a browser window or login using the website.
Preferably, I want the user to simply enter the URL to that page, the login info, and then eventually I want to extract information from the webpage without the user ever having to see a webview...
Furthermore, here is a link to a question posted by my colleague that expresses the same problem: Android link navigation and box input on internet
if you want to "automatically" login the user without having to click login you would have to implement a listener on text changed on the editText
something like this:
editText = (EditText)findViewById(R.id.myEditText);
editText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
//validate the user here
}
but honestly I wouldnt recommend this since you would have be query-ing your server to check everytime the user enters/deletes a single letter. And if I'm right that is not efficient nor and uses a lot of resources (i.e. battery)
And for getting the parameters from the activity previously created use a Bundle on the previous activity and send the extras to the new activity (login activity) and then use the extras on your onCreate, like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
....
}

User leaves activity by going back

Im connected to a chat server which gives me messages i print out in a textview. This continues when the user leaves the application by pressing the home key etc etc. I would like to close all streams if the user goes back with the phones back button to the previous activity. Problem is that onStop() and onPause() are both called independent of if the Home key was pressed or the back key. Its just called when the activity loses focus or visibility, doesnt matter which way it happened.
How do i find out if the back key was pressed, and not home?
You can implement your own version of onBackPressed(). That way you'll know every time it's pressed and can do what ever you need to in that callback. So do something like this:
public void onBackPressed(){
//Do the stuff you want to do
//Then call the parent class version function to allow it to do any other stuff
// that needs to happen as well.
super.onBackPressed();
}

Categories

Resources