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.
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.
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
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 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.
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