How can I preserve data between activities? For instance, I have an edit text and a button in Activity A. I fill in that editText and then click on the button. That button starts a new intent, Activity B. In B there are several buttons, but all of them if clicked on, start activity A again. I want that editText to be still filled in as well as pass specific data to Activity A depending on the button pressed in Activity B.
I know this has to do with onStop() -> onStart(), but couldn't get it to work.
You should start Activity B with startActivityForResult. Then, when u set the result, you should "catch" it in Activity A by overriding onActivityResult().
As for EditText, if you don't call finish() on Activity A - you don't need to override onSaveInstanceState or anything else - system does it for you.
Don't finish the activity in Activity A intent to Activity B. and use startActivityForResult(intent) rather than startActivity(intent) to be able to pass the data back to activity A again. check this answer to how How to manage `startActivityForResult` on Android?.
Related
I have 2 activities which one activity leads to the other activity.
The first activity present a listview and the items click leads to the second activity.
When I click the back button I get back to the first activity but the list reload and scroll up to the first item. I want the list to stay at its place after I get back to it.
If you call finish() in activity A when openig the new Activity B on back press you will call onCreate() of activity A to avoid this avoid calling finish() in activity A in your onItemClickListner()of activity A and record tge position of tge click in Activity A, in which case calling the back press in Activity B will cll for tge onResume() in activity A where in you could call For a direct scroll:
getListView().setSelection(<position>);
Or For a smooth scroll:
getListView().smoothScrollToPosition(<position>);
when you press back you call oncreate() on that activity so everything reload
you could use :
getListView().smoothScrollToPosition(yourpostion);
and think about using fragments for another way around it .
and recyclerview is advised .
If you don't call finish() on Activity A, even if you go to Activity B and come back, Activity A should not call the whole onCreate() again.
If you take a look at the life cycle of Activities, it will put Activity A in onPause() and probably onStop() depending on what you are doing on Activity B and how you defined launchMode in AndroidManifest.xml.
So when you are calling startActivity(..), don't call finish(). Otherwise it will load everything again to draw the Activity A.
Another possible way is using
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
..
}
OR you can use SharedPreference.
Once fetching data from Parse.com is over (like onPostExecute() of AsyncTask), you can read the data passed from Activity B to relocate the user to the list where they were.
EDIT:
Read this article about how to "come back" to the activity you were in, too.
In my android apps i have four activity A->B->C->D and in every activity i call web service then update the list-view and other element from the server so suppose i did delete some data from the database for Activity A and if user access the Activity B and he press the back button then Activity A should be refreshed. I did override onResume method and check the data is updated or not but didn't worked. I also tried another way but not achieve my goal.
my Activity structure is like a following
Class A Is Fragment which is having two tab and each tab contain listview
Class B Is Activity
Class C Is Activity
Class D Is Activity
I don't understand how to call web service again when press the back button and how to update fragment and activity when user press the back button. Please anybody suggest me some answer.
Thanks in advance
you can override the OnBackPressed-method. check the top-answer on that link!!
Android: Proper Way to use onBackPressed() with Toast
Start activity B from activity A with "startActivityForResult" instead of "startActivity".
In activity B you defaultly return RESULT_CANCELED -> so setResult(RESULT_CANCELED). Only when activity B is finished on purpose you return RESULT_OK.
In activity A you then just need to implement onActivityResult(). Depending on the result returned from activity B you can then do specific stuff in activity A, in your case refresh something.
Here is an example: http://www.vogella.com/articles/AndroidIntent/#usingintents_sub
I am new going for the android.I am working on an app in which I have two activities let say A and B.
In activity A I have a list view of some items.I have a button in activity A which takes me to activity B.In activity B I have a seek-bar.
I am using the seek-bar to filter the result of activity A.
I have two buttons in activity B cancel and filter.
After adjusting seek-bar if user clicking filter button than it takes user to activity A and showing filter results.
User can play between activity A and activity B.
Now three different scenario are there for coming back from activity B to A.
By pressing filter button
By pressing cancel button
By pressing phone's back button
After adjusting seek-bar if user pressing filter button then activity A is re ordering and showing filter results. Here I want to save the instance of activity B. so that from activity A if user again going in activity B then I can show the previous state of activity B.(I am able to do this)
In second scenario if user adjusting the seek-bar again and pressing cancel button then Activity A is reordering.Here I do not want to save the instance of activity B and if user again going in activity B from activity A then I want to show the previous state of activity B.(I am not getting how to do this ??)
In Third scenario if user adjusting the seek-bar again and pressing phone's back button then Activity A is reordering but now if user again going in activity B from activity A then activity B is restarting that I do not want, here also I want to show the previous state of activity B.(I am not getting how to do this also ??)
I am stuck with this problem.
Thank you so much in advance.
You must consider each of the cases and manage the activity lifecycle accordingly. You did this just fine, the problem is how to manage it. So, the first step is to study this:
activity and lifecycles
After understanding how the lifecycle of activities is handled by the android os you're on your way: Manage the life of your second activity so that state is mantained by overriding the onPause method (which is called when your activity is no longer in the front of the application) or finish the activity if you don't want to save the state (effectively calling the onStop method.
I would solve this in this way:
in activity A i call the activity B with a startActivityForResult. This way, when the activity B is finished, it's state is not mantained. So, call finish() inside activity B to return to activity A without saving it's state.
When wanting to save the state of activity b, call the activity A so that the onPause method gets called and the state saved.
Hope this clarifies it for you.
am having a two activities that interact.
activity A picks input values and sends to activity B for the user to confirm input before submitting. activity B should allow the user to go back to activity A to edit input values if required or submit the values if they are ok. If user submits values, activity A should be finished and if he edits, then he goes back to activity A.
I have used startActivityForResult() and setResult() methods to kill the activity A when the user submits the values that has worked perfectly, but when i click the edit button to return to activity A, it call the activity A using a new Intent, and subsequently, it starts the activity A, yet previously started activity A is still running . what i want is to resume the activity A where it was left with the existing values before activity B started on click of the back button. How do i achieve this?? Your assistance is highly appreciated
Call finish() in Activity B when you need to edit. There is no need to fire an intent that creates new instance of Activity A. It will resume Activity A.
Hey guys
Activity A fires intent on activity B and then on back pressed of activity B the saved state of Activity A is shown.
I want to show the updated / refreshed state of activity A when back pressed on Activity B
Then refresh your data in onResume() of activity A.
You should override onResume() method in your Activity A and update state there.
If you're using multiple different activities you can also use startActivityForResult() and then perform post-result processing by overriding the onActivityResult method. This also gives you the option to perform different tasks based on the result and the activity that the user was just returned from.