Advice needed with back button on the device - android

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()

Related

Closing the background activity which has the same name

I'm trying to develop an app which has two editText fields for entering location. So I made two java files
1.MapsActivity.java(with activity_maps.xml)
2.AutoComplete.java(with autocomplete.xml)
In MapsActivity while clicking the 'from' text field, it will open the AutoComplete activity and after clicking any of the suggestions in it will open a fresh MapsActivity and places the clicked item . Likewise for 'to' text field also.
I am using Intents to pass the clicked values. So at last, five activities are opened when the two text boxes are filled. When I click back button ,it is showing the auto complete activity used the previous time.
Question:
Is any other way to close a particular activity with same name running behind?
Is there any other way to pass values by super.backpressed(); ?
enter image description here
You can try using startActivityForResult() method. That way you can use the result from the second activity in the first and also you wouldn't need to start a new activity. You can read up on it here startActivityForResult
try to clear backstack/clear_top/single_top using the intent.
Just add/set flags in intent.
or
finish activity after startActivity.
or
you can override the onBackPressed method to call the specific activity.

How do I modify one activity's variables from another activity?

Let's say
First.class
has a variable
String currentValue = "Red"
with a button that leads to Second.class (an activity). First.class(Activity) displays in a textview what the variable currentValue happens to be. (Currently, Red).
If we push the button, it takes us to Second.class, which has an EditText box to modify the variable in First.class. It also has a button to confirm the change. Finally, it has a TextView at the very bottom showing a preview of what First.class' value variable is.
When a user types in "Blue" in Second.class' EditText box and hits the button, how would we change the variable from First.class without using intents and going back to that activity? I want to stay within Second.activity and do the changes from there.
After hitting the confirm button, the preview TextView should update to match the newly modified variable. We should still be seeing Second.class, I remind you. If the user hits "Back" or "up" at this point, they should return to First.class and also see that the TextView in First.class has been changed.
How do I modify First.class' variables if Second.class is entirely separate from First.class and cannot access it? (First.class is the hierarchical parent of Second.class.
How do I modify First.class' variables if Second.class is entirely separate from First.class and cannot access it?
You can't or (more importantly) you should not try to do this.
The Android Activity is a "special case" class and should be generally considered as being self-contained. In other words any changes to data in the second Activity which need to be reflected in the first Activity must be either persisted using some form of global storage (SharedPreferences for example) or should be passed using the extras of an Intent or a Bundle.
With SharedPreferences simply have the first Activity save your currentValue before starting the second Activity and do the reverse in second Activity before returning to the first. The first Activity then simply needs to check the SharedPreferences in onResume() and update its TextView if necessary.
As codeMagic mentioned, however, simply using startActivityForResult(...) would allow passing currentValue from the first to the second Activity and, before the second exits, updating the Bundle with any changes will allow it to be passed back to the first Activity through onActivityResult(...).

how to make the edit text value remain on the same page after comming from one activity

with the topic name everyone should be confused let me explain my question.i have two activity in first activity there is two edit text and two button one button is next and other is set. if i enter some values in the edit text and press the next button it will go so next page. In the second page after making some process and come to the first page when i press a button from second activity the values in the edit text in the first Activity will be disappear.my question that is there any solution to make the edit text value remain same in the page even after coming from the other activity.
Use SharedPreferences to save the values in the edit text when the button is clicked, then reset them in onResume().
You could save your values in your EditTexts like so:
Intent i = new Intent();
i.putStringExtra("editText1","text to save");
and you could start the 2nd activity for a result. When you return from the second activity, you can retrieve the values by using the getStringExtra() method

How to check which activity finished

In my Android application I need to Override the onResume() method to check which of two possible activities just finished. The user will either have entered an amount of money, or named and chosen a percent for a category. How can I do that? Also, if a user presses home and then goes back to my app, is onResume() called? If so, I can just call super.onResume(), right?
I have three classes: PaySaver, NewSavingCategory, and NewPaycheck. PaySaver.java is the main Activity, and there are two buttons: New Paycheck (launches a dialog box where a user inputs $ (NewPaycheck.java)) and New Saving Category (launches a dialogbox where a user inputs a name and a % (NewSavingCategory.java)). When the dialog box is closed via an enter button, I want the main activity to be updated with the information entered.
Thanks!
How can I do that?
Most likely, you don't. Both of those other activities updated your central data model. In onResume(), you update your UI from that same central data model. Hence, it does not matter where the user came from -- you are grabbing the latest data.
Also, if a user presses home and then goes back to my app, is onResume() called?
On the activity they return to, yes.
If so, I can just call super.onResume(), right?
Not only "can" you do that, you have to call super.onResume(), or your activity will crash.

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

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
}

Categories

Resources