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.
Related
I have a problem. According to the drawing, I have an activity that has three fragments, each fragment has its recycleview with the items to choose from, so far so good.
My goal: when I click on an item, it opens another activity, legending it with a variable, so far so good.
When I press the back button, I leave the second activity and go back to the first with the variable, the problem is when I return to the first activity, I tried to trigger a function that analyzes the variable within onResume (), but this function it doesn't work, I repeat, it doesn't work with fragments. What return function works on fragments? Is there another simple alternative?
See the image here
Start your child Activity for result using startActivityForResult() and then in your child Activity set the result, after you have collected your data, using setResult() and manually finish your child Activity using finish(). Then in the Fragment class again, override onActivityResult() to receive the data set in your child Activity.
Refer to this guide for more in depth information: https://developer.android.com/training/basics/intents/result.
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.
I have a list in my app. In another activity, user deletes a row from database and it returns to the main activity (I use finish() here). When it returns back, the row that has been removed from database is still on the list. If user presses refresh button, it disappears.
How can I refresh it automatic?
Override your original Activity's (the one that is showed when the user presses back) onResume(), so it looks like:
#Override
protected void onResume()
{
refreshListMethod();
super.onResume();
}
refreshListMethod(); is whatever approach you chose to refresh the List since you say:
user presses refresh button, it disappears.
So turn that approach into a method so you can use it wherever needed.
Recreate the Activity When you click a list item, you can call finish() on the ListActivity then recreate the Activity with an Intent after you delete the row in the new Activity
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