Using data from different activities - android

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.

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.

Set and load a certain text

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.

How to fill Array on second Activity with elements of EdiText of first Activity?

I have two Activities the first is simple Activity but the second is ListActivity.I have EditText element and a button on the first activity,I want when I type smth. in edittext and click the button,I want to go to the second activity where the information I typed would be as list item,and so on.How to send data from editText to second activity so I could use setOnKeyListener.
You'll want to check out the documentation on Intent, specifically Intent.putExtra():
http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String,%20java.lang.CharSequence%29
This is used to pass aditional data with the intent. In your first activity, you'd put your data as extra into the intent, and in the second activity you'd rerieve that data and do whatever you need to do with it.

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