Android, simply view question about view reset - android

I have a button in my activity, when you click the button an if statement is ran against its text label;
if(BTN_1.getText()=="firsttext"){
//do some stuff, then...
BTN_1.setText("secondtext");
}else if(BTN_1.getText()=="secondtext"){
//do other stuff, then...
BTN_1.setText("firsttext");
}
Firstly, if I hit the home button and go back to the desktop and then click back onto my app the view has reset its self; if I press the button and leave it in a state where the text of the button is "secondtext", when I return to my app it says "firsttext", how can I stop the view of my app refreshing its self like this?
Secondly, under my XML layout I have defined the buttons text; android:text="firsttext"
But this won't actually match my if statement above, under onCreate of this app I have: BTN_CONNECT.setText("Connect");
But visually the text of the button is exactly the same, why won't it match?
Thanks for reading :)

Where do I start?
String compares need to be done with equals(), not ==.
String compares to check your state are bad to begin with. Use an integer/enum to see manage your state.
Depending on how long you want your state to persist, you can either do it in Activity.saveInstanceState() (so it will persist if you change orientation), or in the SharedPreferences if you want it to persist forever.
Don't use hard-coded strings for android:text, use resources, so you can translate them.
I don't understand your last point.

Related

How to change the button functionality after task is executed

In Activity1 have 1 button ,if user clicks on button it goes to Activity2.here i want to change button flow based on result in activity10,i.e if result is OK in Activity10 then Activity1 renders then user clicks on button it should show activity5(After reopened the app also app should show this result).I tried by using Bundle but when i reopened the app first scenario executing,it can be achieved by using shared-preference by taking a Boolean variable but dont know how,suggest me with piece of code to handle this multiple activities with shared preference?help me
From any place you can do
int prefVal = PreferenceManager.getDefaultSharedPreferences().getInt("myint", defVal);
and
PreferenceManager.getDefaultSharedPreferences().edit().put("myint", 100).commit();
Based on the last stored value you, probably, can figure out what to display.

How to save the state of the only Activity in an Android app when the back button is pressed?

I'm making an app that has just one Activity. When I press the Home or Recent Apps button, the state is correctly saved and when I go back to the app everything is there. When I press the Back button and return to the app later, savedInstanceState is always null in onCreate. I've read that the state is not saved when the back button is pressed, but this seems unintuitive in my case considering that the back and home buttons are equally valid ways to leave the app in the user's eyes, until they try and go back to what they were doing. Is there no way to save state when the back button is pressed?
If I must do some persistent storage, then:
a) How do I override the back button behaviour to make it save the app when back is pressed, and
b) Can I persistently save a Bundle somehow so that I don't have to duplicate the code for state saving?
What you are searching for is SharedPreferences I think take a look at it. It will help you solving your problem
I may be wrong, but my understanding is that savedInstanceState only saves your data when you change your device orientation, but as soon as you click back button, your activity is destroyed. In order to save your data when back button is pressed, you could save your data by using SharePreferences or you could use singleton class.
http://developer.android.com/reference/android/app/Activity.html take a look at an activity lifecycle first. Sounds to me like you need to override the ondestroy method and persist your stuff.you can use a bundle for simple stuff but complex stuff it's best to use parcel.
The oncreate method should check stuff to see if it's null

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

Button stays disabled until second click?

Good Morning,
I have a list of items with a prev. & next button. When the user is at the start of the list the prev button is disabled. Clicking next takes them to the next record and my click handler sets the prev button enabled true. However in the emulator it doesn't show the button enabled. Clicking next moves me to the third record and again the handler sets the prev button enabled but this time it does become enabled in the emulator. I'm grasping at straw here but do I need to invalidate and redraw or something?? I don't understand why such an elementary task is not working.
In XML:
<Button
android:id="#+id/btn_PrevLift"
...
android:enabled="false"
android:onClick="btn_PrevLiftClick" />
In the handler code:
private void UpdateNavButtonStatus(int z)
{
...
btn_Next.setEnabled(true);
btn_Prev.setEnabled(true);
....
}
No just to show you how little I know about what I'm doing how come when I look at the variable values in Eclipse debug I can't see the enabled property in any state???
More Info
Very odd to me at least. If I move from using XML defined event handlers to programatically defined as below it works great!!!???
btn_Nxt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Call helper methods etc...
}
});
I think I have it but not sure exactly why
So when I was trying to get a handle on my Button objects I was using the View.findViewById(etc). When I changed from XML to the programatically declared event handler I used ViewGroup.findViewById. Reverting back to xml if I use the ViewGroup I get a "different" handle that seems to work...????
Ok,so what you can do is declare a variable count and on the click f button next increment the value of count..and on the click of Back button decrement the value of count and give the condition that while count<0 the task of the back button is done else nothing
I have identified the problem and of course the problem is me. I was enabling the button and then the ListView was updating to the next record. Of course my enabled button was one behind so it appeared that when I clicked a second time the button suddenly enable but that is not the case. When I clicked a second time we moved to record three and showed button for record two which was enabled.
See my buttons were part of my ListView and new buttons were being drawn for each record. OOHHHH it makes so much sense now how everything was behaving.
Anyway I moved the buttons off the ListView layout so they remain constant as the user navigates through the records.

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

Categories

Resources