Set and load a certain text - android

I'm currently programming an app where you should enter a text which is stored. The text should be able to be loaded in the app (maybe another activity) then.
It should be able to be used this way:
task.execute(new String[]{city,lang});
Is this makeable?
Thanks

You can use an Edittext on your first Activity on which user can type something.You can get that "something" programmatically in the same activity as a string.Now if you want to avail that string to another activity then you can use an Intent from the first activity to the second activity which will avail that string in the second activity.

Related

send & save data to a List view in android

i want to create an app that have two activity's
the first activity have one EditText to write String on it & one Button to Send String that is in EditText
and activity two have one ListView to receive String from EditText & show it...
for example the ListView can show 20 String received from activity one
i have no problem with send strings between activity
but i don't no how to get them & put them to ListView to show!
if you know about send & save data to a List view please help me :)
sorry for my English
Thanks
You can't push 1 value to an Activity, start the activity, show a ListView, go back and add more. The second Activity will typically restart with a clean state.
Your options include
building an Arraylist of all the items, then starting the new Activity while passing that list
Use Fragments to achieve a similar task as (1), but the hosting Activity holds the list
Use persistent storage like Sqlite to store individual records, start the second activity whenever, and use a CursorAdapter to display the data to a ListView

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.

Using data from different activities

I have 2 Activities namely MainActivity and SubActivity. How do i make that when onCreate of my MainActivity, the EditText in SubActivity will be checked if it has a text?
If it don't have a text, I want my SubActivity to start so I can input a text.
Firstly in the starting of Main Activity check by using If statement whether there is some text or not if there is text remain in that activity and if not then in the else statement use intent and call Sub Activity to insert the text .
Lets assume you have not input anything to your sub activity's edittext(its empty) then it will open the sub activity and ask you for the text.
After entering the text to sub activity's edittext you have to store it. (Otherwise whats the use of that text.)
So make a static string variable which stores the value from your sub activity's edittext.
Now in your main activity instead of checking the sub activity's edittext just check the static string variable which you have created.
if it is empty
call the sub activity otherwise go with main activity.
I hope i have understood your problem.

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

Categories

Resources