I have a ListActivity with an EditText and a ListView. Content of the ListView changes according to the EditText content. I do the following steps:
1) Fill in something in the EditText
2) Consequently the Listview changes (properly)
3) I scroll the listview
4) I press the back button
At this point I expect to get back to the previous activity, while i happens that the EditText gets erased and consequently the ListView empty.
Is there a way to tell the EditText not to erase itself when Back Button is pressed?
Thanks a lot.
G
You could save the editText in the Shared Preferences. To always have the text saved when you back or when you open again the application.Take a look : Shared Preferences
Like this:
private SharedPreferences pref;
In your SecondClass:
pref.edit().putString("text", Text).commit();
MainClass:
String texto = pref.getString("text", "");
I just want the activity to finish whenever i press back button. Doesn't matter what i was doing and which object had the focus
Here's how you can force a finish of the Activity when back is pressed...
#Override
public void onBackPressed() {
finish();
}
You can use the onPause method to store the EditText value. Either in SharedPrefrence or a Global variable.
#Override
public void onPause() {
super.onPause();
// Get the current value from Edit text and store the value on sharedpref or global variable
}
here inside onPause method you can save the current value of the EditText into shared preference.
alternatively you can overide the back button's event and do the same as below
you can use following method
#Override
public void onBackPressed(){
//your code here to save it to shared pref or global varialble then invoke relevant activity manually
}
to overide the events when the back button is pressed
Related
I am just trying to change the value of a Preference when the PreferenceActivity has been opened. As there is no "setValue" or similar on a Preference, I try
My code:
long value = System.currentTimeMillis()/1000;
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString("test",""+value).apply();
getPreferenceScreen().findPreference("test").setSummary(""+value);
My XML:
<EditTextPreference
android:key="test" />
What I expect:
When clicking on my Preference, it should display the value of time (same than summary) and let me edit it.
What happen:
The value is only changed after I closed the Activity. Next time I open the screen, the value is correct (but as in fact already changed to the next one)
First attempt:
Let s say value is 1521143527. Correctly written in the summary, but when I click on the Preference, the popup display an empty value.
Second attemp:
Summary has changed to 1521143540. When I click on Preference, I can edit the previous value (1521143527)
Third attempt:
New Summary, but Preference value is not changed and is still: 1521143540
etc...
Any idea what is wrong?
DIRTY WORKAROUND:
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.preferences);
Will now force the preference to update, but that's really dirty, and I still don't understand...
If you look into the PreferenceFragment source code ,you can see that there is a method called bindPreferences() which binds the preference values to Views . Only in 2 scenarios this method is called,
When Activity created onActivityCreated(#Nullable Bundle savedInstanceState)
When addPreferencesFromResource() called. there is a handler which triggers the bindPreferences()
Other than this there is no way the views are updated. bindPreferences() is a private method , so you can't call this method outside of the class. So you should update your preferences before either those events.
You mentioned, As a workaround solution, you should update your preference first then call addPreferencesFromResource() . Like below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long value = System.currentTimeMillis()/1000;
getPreferenceManager().getSharedPreferences().edit().putString("test",""+value).commit();
addPreferencesFromResource(R.xml.pref_general);
getPreferenceScreen().findPreference("test").setSummary(""+value);
}
Here is my question,my app flow is screen1,screen2,screen3.and their content views are like this:
screen1.java-->screen1.xml
screen2.java-->screen3.xml
screen3.java-->screen3.xml
here in screen1 if user clicks on toggle button to is going to screen2 then screen3 in screen3 payment success then only screen1 toggle button should change,how to achieve this,didn't get any idea,plz help me,Thanks.
You can save the state of toggle button in Shared preferences.
Check this : Shared Preferences Android and Example
Hope this helps.
Use a static global array list and save the pressed states at their positions in that list. In the next activities use that array list to set the toggle status of your buttons
This is similarly like saving the checked states of a checkbox in a listview
You can pass button state using Bundle between activities like following
Start activity 2
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_NAME, VALUE);
startActivity(intent);
Get that value in activity 2 like
#Override
protected void onCreate(Bundle savedInstanceState) {
....
boolean value = getIntent().getExtras().getBoolean(EXTRA_VALUE);
}
same like above you can pass is to Activity 3.
Or
You can make a static variable in you Activity 1 and then access that from Activity 3.
You can make that toggle button static and then you can change its state from any activity.
static ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggle_btn);
but you need to be careful.
You could also pass the toggle state via intents and onActivityResult().
Here is a video tutorial on intents
Here is some a tutorial on onActivityResult
i am developing an android application. i have 8 activities in the application. i have a submit button in the last activity. the user can go back to any activity and edit the data before pressing submit.i have two buttons in every activity named as Next and previous. and in the last activity an extra button called Submit is there. I want to come to any previous page from the next page and edit the data and go to submit.
Now my problem is, every time i press Previous my previous-activity data fields are becoming blank..
please help me solving this problem.
Save the state of your activities with bundles. The back button ends the lifecycle of an activity. Here's a link that would guide you through what you should do. Hope this helps.
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
Dont kill your activity while moving to other activity. Also you can set a flag while launching activity whihc will just put your activity to backstack and will be retrieved when relaunch by pressing back. Follow below link for details:
http://developer.android.com/guide/components/tasks-and-back-stack.html
You need to save the state of Activity:
private static final String INPUT1 = "ip1";
private static final String INPUT2 = "ip2";
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putString(INPUT1, mEditText1.getText().toString());
savedInstanceState.putString(INPUT2, mEditText2.getText().toString());
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
And restore it when Activity starts:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Always call the superclass first
mEditText1 = findViewById(.....
mEditText2 = findViewById(.....
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mEditText1.setText(savedInstanceState.getString(INPUT1));
mEditText2.setText(savedInstanceState.getString(INPUT2));
}
}
Also, you can tell EditText and other such Views to save their state individually, by adding android:saveEnabled="true" to their layout:
<EditText
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:saveEnabled="true"
android:inputType="text"/>
Everything in my Activity is working fine. I'm able to load all the values fine and show in every EditText except one. The sequence of the actions on onCreate is
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rule_editor);
initComponents(); //references all private objects to each object in activity layout
populateCategorySpinner(); //populates a category spinner
loadRule(); //loads the rule from a singleton on the basis of an extra data passed from parent activity
attachEvents(); //attaches event to each object
Log.d("", txtIdentifierString.getText().toString());
}
The problem is in one of the EditText. The reference in code is txtIdentifierString. The values that I set (by setText()) on it is not showing up in activity on runtime.
Logcat shows up a value from the Log.d method call in last line of the onCreate method, but its not visible in the EditText txtIdentifierString. The EditText box is visible, I can focus on it and type in a value as well.
Does anyone has any idea about it?
I'm answering my own question.
I debugged the codes and found out that OnItemSelectedListener on a spinner had to reset the EditText in question. The change is triggered before I Logged the value
Actually in my app i have a button on an listView..now on click of that button i have done some changes..so when i move to previous activity after than this changes should appear on that activity..but in my case the changes occur but not appears after i exit from the activity where my listView Button is present..so how can i do that so that my changes occur immediately after i exit from my first activity..code i have wrritten:
code for ListView Button Onclick:
public boolean stopCycleStage(View v)
{
Button butStop=(Button) findViewById(R.id.butStop);
TextView setStopTxtViewTitle =(TextView)findViewById(R.id.setStopTxtViewTitle);
Date currentDate=new Date();
int iStopStartCount = CycleManager.getSingletonObject().getStopStartCount();
Date dtStopDate = currentDate;
CycleManager.getSingletonObject().setStopStartDate(dtStopDate, iStopStartCount);
Date dtStart = CycleManager.getSingletonObject().getStartDate();
if (dtStopDate.getTime() == dtStart.getTime())
CycleManager.getSingletonObject().removeHistoryDate(dtStart);
butStop.setBackgroundResource(R.drawable.settings_but_disabled);
setStopTxtViewTitle.setTextColor(Color.parseColor("#808080"));
return true;
}
In your first activity, you should refresh the view in the onResume function rather than just in the onStart or onCreate.
Refer to the activity documentation to see the lifecycle of an activity
PS: this is just a guess because you have not given enough code to show how you load the data in your 1st acitvity.
Id use onActivityResult() in the waiting activity, and in there I would redraw the elements that should have changed when you clicked your button in the child activity. In any case, onActivityResult() is the correct way to go unless you are not waiting for any data from child activity, then I'd use onResume().
See http://developer.android.com/reference/android/app/Activity.html#StartingActivities for information on what you should use in your particular situation (you didn't exactly give much information :))