Refreshing parent activity on going back from current activity - android

I am trying to refresh the contents of the previous activity when i am going back from current activity to the just previous or parent activity. How to do this?

When you start the child activity the original activity will receive a call to onSaveInstanceState. It can save any information it needs to in the Bundle passed to that call.
When the Parent activity is restarted, the Bundle created by onSaveInstanceState will be passed to the Activities onCreate method which can use the information as necessary to put the Activity back into the desired state.
This Bundle will also be passed to the onRestoreInstanceState() method of the parent activity so you may wish to use the data there instead.
To read more about this in the Android documentation, take a look at this page
If the Parent activity needs to change state as a result of actions in the child Activity it can implement onActivityResult to receive the data passed back by the child activity as described on this page

It is unclear from question what kind of data you have. You can use adapter.notifyDataSetChanged() on Ui thread inside onResume() to refresh data.

Related

Android prevent restart activity after startActivity

I have a problem with startActivity() methods.
Problem Structure
Click Link (example, http://google.com) in some TextView, using LinkMovementMethod
Callback into LinkCatcher class (because some TextView exist almost all activity, so i using outer class)
Post event to MainActivity using Otto (because it prevent startActivity outside of activity problem.)
on MainActivity, intent to Activity B without any Flag, just one extra (link)
When Activity B created, Activity A will destroy and call onCreate(Bundle savedInstanceState) (i insert some Log.e to my Code, definitely it re-call Log code)
Tried Methods
Insert android:launchMode="singleInstance" in MainActivity, Activity B
Insert android:configChanges="orientation|screenSize|keyboardHidden" in MainActivity (it means, orientation is not problem. i already implement onSaveInstanceState in all activity of my Application)
3.remove Step 3. instead of Post event to MainActivity, call startActiivty() in LinkCatcher Class
I tried all methods which i try it. and i don't know why activity is destroy and re-call onCreate (not onResume) even i don't use any finish() methods.
Because that's how Android is designed- at any moment an Activity not currently on screen can be killed. You can't prevent it. What you can do is account for it- you can save off any necessary data in onSaveInstanceState, and restore it in onCreate from the Bundle or in onRestoreInstanceState.

Update View with updated data in Android

I am noob in Android. Suppose I have two Activity in my android app.
Activity A and B, currently I am on Activity A then on click on one button I am now Activity B. Here From Activity B I want to update some view with updated data that is in Activity A. I got updated data in Activity B so Should I use here LocalBroadCastReceiver for this or anything ?? so that When I press back then app should show me updated data in Activity A
Should we use our custom callback here to update the UI view data of Activity A from Activity B ??
No, you shouldn't use BroadcastReceiver for that. The method depends on the size of data you want to transfer from Activity B to Activity A. If it's quite small, you can start Activity B with startActivityForResult and then get data back at the onActivityResult method (in Activity B you should use setResult once you are done). If the size of data is quite big, it's better to use DB for storing it instead of keeping it in memory.
okay , there are multiple ways to do it :-
you can use a static variable to store you data that too of application level ( might not be a good approach ) check the value of vairable in onResume() and set it to the view.
you can use sharedpreferences if your data is not that large , store the data in Activity B and fetch it on onResume() method of activity A.
as #nikis has told you
if your data is too large store it in db.
I dont think broadcastreceiver fits right in your scenario !!

Activity being destroyed?

Very new to Android but when I am, say, in activity A, and I intend to move to activity B, then go back to activity A, all the data displayed in activity A before going to to B is lost. I never call finish(), am I doing something wrong?
I recommend going through Google's training and learning about activity life cycles.
http://developer.android.com/training/basics/firstapp/index.html
With your two activities, override all of the life cycle methods (onCreate, onResume, etc) and put a simple log message in them. Just move between the activities, and watch your log to learn the order of events.
From there, learn about Bundles and how to save your instance states and you'll be well on your way to making apps!
Check that all your Views do have identifiers in your XML. Android restores only those views which have identifiers.
Additionally, for your custom data (not views):
It can be that your Activity A got destroyed while in background.
Normally they do like that:
1) Add onSaveInstanceState() to your Activity. There put all your custom data in the Bundle argument. Be sure call super at the end. You do not need to store your views, just your custom data such as your custom variables etc.
2) in onCreate see if the savedInstanceState Bundle argument is null.
3) If null populate with your default data.
4) If not null restore the data from the Bundle and populate with them

Where to call notifyOnDataSetChanged

In my code, I have two activities.
One is a list view activity, the other activity is being used to modify the data.
After the data is modified the user is returned to the list view activity.
My question is, where should I call notifyOnDataSetChanged?
Should I call it in the 'resume' method of the list view activity, so that the list is refreshed whenever it is displayed?
Or should I call it from the activity that modifies the data?
I would not know how to do the latter, as the adapter belongs to the list view activity, not to the other activity.
Thanks.
You should call it in the ListView Activity, the method basically queues the list to refresh so it should happen when you get back to the list. Also, as it is an adapter method, you can better guarantee that an instance of the list affected by the adapter is available.
Depending on how you handle your Activities, proabably call it in onResume() or some other method of your ListViewActivity so you will have access to your adapter.
However, another way would be to use startActivityForResult() in your ListViewAcitvity then call notifyOnDataSetChanged() in onActivityResult(), if that is an option you have

onSaveInstanceState() in activity and fragments?

I have an activity which contains 2 fragments. I want to save certain state of the activity and also of the fragments, in order to restore if the activity, or the fragments, are destroyed.
So I'm using onSaveInstanceState both in the fragments and in the activity, and take the data of the bundle passed to onCreate or onCreateView.
This works well besides, when the activity is destroyed. Then in restores it's own data, but, since in onCreate() I instantiate the adapter and the fragments again, they get no state.
How do I solve this?
Thanks in advance.
Most likely the cause of this, is that the Fragment's onCreateView() runs before the Activity's onResume() according to the Fragment lifecycle documentation:
http://developer.android.com/guide/components/fragments.html#Creating

Categories

Resources