i have created dynamic table-layout. in that layout in each row have four buttons.one button is use to navigate from one activity to another. my problem is when i switch from 1st activity to second activity by pressing button and i come back from 2nd activity to 1st activity clicked button loose its state.
Ex. 1) in 1st Activity i click button 1 and check the check box and go to 2nd activity after doing some operation i come back to 1st activity and i see the button1 and check box are unclicked and unchecked.
so how to maintain states of buttons
obviously it would be happen because i have to maintain the check box state when its come on the on resume() ..u can take a shared preferance to mantain the check box state .
Use variables and store the values in the variable when u clicked the button. Store that variable values in the Bundle object using the bundle.putExtras...() method. send this bundle object to the other activity using Intent.putExtra...s();. In the same manner ,if u need the those values then resend that bundle object and get that bundle object using Intent.getExtras....();. And finally restore the values from the bundle object.
This information may solve your problem.
Related
Reqirement-I have an activity with three fragments. Each fragment has a form for user to fill and a proceed button.Proceed button will move user to next fragment.On third fragment proceed button will send data filled in the form of all three fragments to server.
Approach- I declared an object in activity.In each fragment when user click on proceed I assign fields of that object with data filled in fragment.When user click on proceed button in third fragment that object is submitted to server.
Is this approach correct? Is there any better way of doing this?
You could use Shared Preferences instead if you don't want to maintain an object and want to retain the information the user entered for later use in the app.
Working on main activity thur open fragment.
Create home activity one static method. and call after one fragment button clicked.
i have attach below step Please check.
1.Home activity create one method.
2.method name create addUserLineInfoFragment
3."A fragment" clicked set this code
((YourActivityName)getActivity()).addUserLineInfoFragment();
4.Home activity this method "addUserLineInfoFragment" set "B fragment" open code
i am struggling to keep data or items of list view when i leave one activity to another, for my example i created simple app on click will add number increased to a list view so each click create an item like 1 another click add 2 and so on.
the program works fin for main activity but then i would like to see the result on another activity i call it second activity but the problem is when i hit back button on second activity to go back to main activity, i will lose all of items on the list view.
i was looking on google so many information but could not get my head around it, so please advice and please be little more in detail as i am beginner, i think instance state or shared preference will do the job but i do not know any of them
thanks in advance and here is my app code for main activity and second activity and picture for output
sorry i add code as images becausethe site keep saying the code need indentation thank you
main activity[main out put][2]second activity[second activity out put][4]
You need to save the data of the ListView in some form, either in a file or in a database (local or remote).There is no direct way to store list view, but you can store the data from the list view and then set it in to the ListView later when you switch back to the activity.
You need to keep in mind that switching activity results to invoking of onPause() method in android, and if the data is not saved in the current activity then it will be lost when you move on to another activity.
Add all your values into the array, pass it to the adapter, then after clicking on the list view item, make an intent where you want to switch your activity (means from one activity to second activity), while implemented the intent, pass your array also, using intent.put extra. Then you will get your array in your second activity and coming back to your previous activity, again pass an intent (with your array) and get your array in your previous activity and pass it into the adapter.
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(...).
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
I have 2 Activities A and B. Now these are my objectives.
When I'm in B and if I press the Home button, the state of the Activity should be saved. (No problem with this.)
When I start B from A after step 1 a new instance of B should be created (i.e) Previous state should be discarded.
But in Step 2 the state of B still prevails. How do I accomplish my objective?
I think one possible solution woudl be to pass some extra information inside the starting Intent, when you start Activity B from A (like a boolean value). And in the "onStart()" of B, you check if you can find this extra info in the intent (you get it with getIntent()). If it's not present, that means you do reload the activity's previous state. If it is, then you don't reload it.
refer this url
Android; How can I initialise state in one activity, then have another refresh that?
You don't even need to send a boolean like Scythe suggested. The Bundle savedInstanceState will be null in onCreate for Activity B if Activity A just started it, whereas it will be non-null if you are coming back from a saved state.