I am having two activities (activity1 and activiy2) and each activity is having one button each. In activity1 i having a spinner with few options. Suppose i am selecting option 2 from this spinner and i am clicking the button in activity1, then activity2 starts. When i click back button activity1 is resumed and the same option 2 is visible (like i need). Now the problem is that if my activity2 is started and i am clicking a button in it, activity1 is started. But instead of resuming the previous state of activity1 it starts in a way that it has just created and the previous selection is changed. How can i get the same facility like back button (not the facility of going back to previous activity, i mean automatically resuming the previous state of any activity) even when i start the activity again. Simply i need to know how to maintain the previous state of an activity if it is again visited.
It is with this code I go from one activity to another when button is clicked:
Intent intent=new Intent();
intent.setClassName(getApplicationContext(),"com.myapp.activityname");
startActivity(intent);
Kindly help me.I am a beginner in android, so if any one is giving the answer please explain it a bit. Thanks in adavnce
Think I found the answer. Let me tell what I have done in simple words,
Suppose i am having two activities activity1 and activity2 and i am navigating from activity1 to activity2(i have done some works in activity2) and again back to activity 1 by clicking on a button in activity1. Now at this stage I wanted to go back to activity2 and i want to see my activity2 in the same condition when I last left activity2.
For the above scenario what i have done is that in the manifest i made some changes like this:
And in the activity1 on the button click event i have done like this:
And in activity2 on button click event i have done like this:
Now what will happen is that whatever the changes we have made in the activity2 will not be lost, and we can view activity2 in the same state as we left previously.
I believe this is the answer and this works fine for me.
By overriding an Activity’s
onSaveInstanceState
event handler, you can use its Bundle parameter to save instance values. Here is an example:The saved Bundle is passed into the
onRestoreInstanceState
andonCreate
methods if the application is forced to restart during a session. You can then extract values from the Bundle and use them to update the Activity instance state. Here is an example:It’s important to remember that
onSaveInstanceState
is called only when an Activity becomes inactive, but not when it is being closed by a call tofinish
or by the user pressing the Back button.Download source code or see the entire post
For activity one, you need to use the startActivityForResult() function that returns values to the parent activity which is the activity one.
And to receive the values from activity two without alteration of activity one, add the following code
Activity 2
add the following code to return to activity one and pass some values
Finally you are done