I have an Activity_A where there is a ListView whose data depends on a value say dependency
From this Activity_B user goes to Activity_B where he can change the value of dependency
since this dependency must change the data in the ListView i need to reload the ListView, hence i did this:
if(//dependency is changed){
Intent intent = new Intent(Activity_B.this,Activity_A.class);
startActivity(intent);
}
the ListView is populated with new data.
Problem:
when i press device back button from this newly loaded Activity_A twice, I end up in Activit_A with previous ListView data. So, if i try to click any item i get this Exception
The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread
How to avoid this??
Thank You
You can store dependency value in SharedPreferences and populate your list based on the dependency value in the Activity_A onResume()
To go back to the previous activity you should not call new intent. Just call finish() on your ACtivity B. Then in your onResume() of Activity A, update the listview with the new values.
Here you are calling Activity A as a new Activity, and the previous Activity A is still in the backstack. So when you press back, you will reach the ACtivity A, which was your first Activity. But the values populating the listview has changed. So when you try to click on anything, exception occurs. Thats the problem.
So when you change the values to the listview, call finish() in Activity B. It will take you back to Activity A. In onResume() of Activity A, update the listview.
In Activity A You have a ListView
in Activity A, onResume() method you set adapter for your ListView.
when you navigates to Activity_B through Intent and change the value of
dependency. on buton click don't start again Activity A just finish the
Activity_B. Then automatically onResume() method of Activity A will be called and
again listview is loaded. Hence changes effect to values of listview
note : Don't forget to declare dependency as static.
Related
I have a listView in an activity. listView contains 2 buttons and one field that gets updated when button is clicked. Onclick on a listView open a new activity for same item (detail description of item).
In new activity again i have 2 buttons and one field that gets updated when button is clicked.
The problem is after updating in detailed activity if user back press button to go to previous activity, updated field value does not reflected there.
How to send notifyDataSetChanged from detailed activity to previous activity?
Is public void onResume() is the place where we can do some trick?
Star second activity for result
startActivityForResult(...)
then catch the result in
onActivityResult(...)
From there you can call notifyDataSetChanged by comparing request & result code
You can indeed call notifyDataSetChanged() in onResume(), although I would suggest that you use startActivityForResult() instead. Once the user presses back you get onActivityResult() (with resultCode == RESULT_CANCELED) where you can call notifyDataSetChanged().
This latter approach has the advantage that you can pass some data from the second activity to the first (in an Intent), for example a boolean that tell whether to notify the adapter. Then you check this data in onActivityResult() and decide what to do.
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.
I have two Activities. One extends Fragment activity and displays some data in a list (fragment1) and details if one clicks on a list item (fragment2). Fragment2 has a menu item that can be clicked to edit that data set. If the user wants to edit the data fragment2 calls the other activity per startActivityForResult in which he can edit the data set.
Should I check in the edit activity if it was invoked for a result by getCallingActivity or should I setResult anyway? (I finish() the activity after saving to restart the activity in order to clear the EditText views, since one can edit or insert new data sets in that activity.)
You can go ahead and just call setResult() without checking the calling Activity. The Activity that doesn't care about the result will simply ignore it with no harm done.
The android app has two activities.
First: MainActivity
Second: AddNewDataActivity
The MainActivityhas a ListView. The user can create a new list view item by clicking on an "Add" button. When the user clicks on the Add button, a AddNewDataActivity is opened and the user fills in some details and saves the work done. Upon saving the work, the activity is finished and the MainActivity comes to front.
Now, before the MainActivity comes to front, I want to update the items in ListView.
For this From your MainActivity start the AddNewDataActivity Activity by calling StartActivityForResult() method not startActivity() and in your AddNewDataActivity activity when you finish call the method setResult() and pass the data in the Intent which will call the onActivityResult() method in your MainActivity here update your listview..
here check the example of startActivityForResult() example
Yous should serialize the Object you create in AddNewActivity and pass it to the main activity as result.
1) start second activity with startActivityForResult(Intent);
2) before calling finish on second activity serialize the object (as json) and put it as bundle extra to the result via setResult();
3) on MainActivity overriede onActivityResult and parse the data from result and add the object to your ArrayList or any data structure you use.
There is another way to do it. Using a singleton where u will have the ArrayList with data and before closing second activity add the object to that ArrayList, and int onActivityResult just refresh the ListView
Here is how to handle onActivityResult
Transfering data between Activities
I want to add an item to a listview that is present in a different activity from the current activity. Say I have a button in my current activity and when this button is clicked I want to add an item to the listview present in a different activity. Can anybody suggest me a work around for this? Thank you.
You can use Intents.
You have to launch your 2nd Activity with a startActivityForResult (Intent intent, int requestCode). Then before terminating it you must put the data that you wish to add to the 1st Activity in an Intent.
Finally, in your first Activity, you get this data in the onActivityResult() method.
Here's a detailled example: http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html
you should use startActivityForResult(for start SelectorActivity) in current activity(lets call it ListActivity)
then in second activity (lets call it SelectorActivity) you have to put some confirm button(OK) and when you click OK button you should call setResult and finish SelectorActivity
back in ListActivity you should override onActivityResult and add data from SelectorActivity to list Adapter
first look here http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Edit: you should return data in Intent ... use Intent.putExtra() in setResult and then Intent.getExtra() in onActivityResult
When the button is clicked store the data in shared preference and when u are starting the list activity get the data from shared preference and populate the list delete the data from the shared preference