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
Related
When iam moving current activity to list activity in list activity have 8 items and in list activity have add button widget when I added one item using add button so total 9 items in list activity. Here my problem is when I click on back button my application not moving to previous activity showing list activity with 8 items after click again back button moving previous activity how to solve this issue without using intent
override onBackPressed() method the way you want.
When you use realtime database like Firebase at the moment you will have updated list. I think when you click on button you are opening new activity and don't finish last one. So you come back to last view again. Of course if you send your code, you will get proper answer.
You should override method onBackPressed() in the activity where your back button is not working correctly for you:
#Override
public void onBackPressed()
{
finish();
super.onBackPressed();
}
I think that adding method call finish() should solve your problem.
In my app I want to allow the user to insert some data which later appears in a list. This aspect is organized as follows:
From the Main Activity, the user can press a button which opens the Editor Activity. Here it's possible to construct the data. Pressing the confirm button, the built data are showed in another activity (New Element Activity), in a list.
Well, if the user goes back through the designated button from New Element Activity to Main Activity I want the list to be cancelled, while now, when adding a new element, the former is still there (that's because the array with the data was initialized before onCreate() method - I don't want to change it). I tried to override the onBackPressed() method, but it didn't work. Suggestions?
Here's my code - the onBackPressed() method.
#Override
public void onBackPressed(){
super.onBackPressed();
data.clear();
Log.d("onBackPressed", "called");
}
Clicking the back button just returns to the previous activity. The overridden method doesn't get called.
Thanks in advance
#Override
public void onBackPressed() {
startActivity(new Intent(CurrentActivity.this,NextActivity.class));
}
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 had created an android app which has 3 activity (A,B,C) in activity A there is 2 autocomplete textview which fetch data from database and button after selecting data in autocomplete text user press button and it goes to next activity which show listview.
Now the problem is when I press the devices back button from listview is display activity A with selected data in autotext how can I avoid the selected data when I press device back button.
Thanks for helping in advance
When you press the back button, activity A will "resume". So, in your Activity A override "onResume"...in the onResume method, clear your fields.
There is nothing with the listactivity's back button as you are trying to clear you field in the previous activity. As you are not finishing your previous activity while creating the list activity so the previous activity is not changing. So while you are returing from the list you are saying the same data as before. to do so as #Dave suggested you could have done the clearing in the oResume method or onPause method. But there is a problem. If you clear your data in onResume or in onPause you data will be cleared for other pausing or resuming reasons like pressing home, or for other applications etc. So you can do any of two
Option 1:
clear the data of the previous activity when you are starting the list activity
or
Option 2:
instead of startActivity call startActiviyForResult and also override onActiviyResult method. So there you can detect when you are returning from list activity. Then clear the data.
To deal with this problem, I would simply override onBackPressed() method, and in there, I would have cleared activity stack as there is no logical explanation to keep activities in memory, and then start a fresh intent to your original activity.
Here is code.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity_A.class); //I suppose they are in same package
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
I hope it helps.
protected void onResume(){
super.onResume();
setContentView(R.layout.activity_main);
}
this also works well..
you should implement startActivityforResut
When you come back to this activity on result you can clear the data