I have a strange problem, I have a few different items in my ListView, which are enabled/disabled based on a preference. They show up in their enabled/disabled state correctly based on the preference, but if they were visible in the list when you open up the preference activity, they wont go to their correct state until they move off screen and back
I've tried calling getListView().refreshDrawableState() onResume of the main activity that the ListView is in, but it didn't seem to do anything.
#Override
public void onResume(){
super.onResume();
getListView().refreshDrawableState();
}
Related
Can't set active item of BottomNavigationView while recreating app: the last selected item is active after recreating app, so BottomNavigationView.setSelectedItemId(R.id.item0) takes no visual effect, but onNavigationItemSelected was called succeessfully.
So how can I prevent restoring previous active item of BNV? Thanks.
You need to do that inside onResume() instead because while your app in the recent apps then the BottomNavigationView will always catch the position.
Why set position not work?
Because you set the position inside onCreate() which not invoked when app start from recent apps.
How to set the position then?
Inside in onStart() or onResume() and that will work.
How to check if app opened from recent-apps?
We can play with boolean e.g:
boolean isFromRecents = false;
onCreate(){
isFromRecents = true;
}
onResume(){
if(isFromRecents)
//do smth
}
I am trying to create an application in which my Fragment contains one listview which is populated from a database, and when one of the item is clicked, a new activity starts.
In this new activity, I update the database, and then come back to the first Fragment (by pressing back button).
What happens next is that the ListView in the first Fragment does not take the changes I made in the second Activity into account.
And strange thing is that when I start the application again, the Fragment shows the updated list.
How can I automatically update the ListView when making changes to the database?
Try to use the onResume( ) method in your Fragment, so you can told the Adapter that the Dataset has changed.
#Override
protected void onResume() {
super.onResume();
// Dataset has changed, notify adapter !
mAdapter.notifyDataSetChanged();
}
I have an activity MainActivity there are three fragments associated with this activity.
Now one of my fragment Timeline has a listview. Which I populate from a Database in the backend. I use an AsyncTask to fetch values from the DB and process them to the List. I trigger this AsyncTask in the onCreate of the Fragment Timeline.
Now from Timeline on click of any list item I navigate to a different Activity called as DetailActivity
The problem is whenever I press back from the DetailActivity the onCreate of my MainActivity is called and my list refreshes again - the whole DB operation is called again and my list does not retain its state.
I am calculating the visible items of my List before I navigate away from the Fragment but I am forced to use static values for these variables so that I retain the position. How to avoid this?
Below are the snippets of my onPause and onResume as laid down in the fragment Timeline
static int index;
static int top;
#Override
public void onPause(){
System.out.println("onPause");
index = lv.getFirstVisiblePosition();
View v = lv.getChildAt(0);
top = (v == null) ? 0 : v.getTop();
super.onPause();
uiHelper.onPause();
}
#Override
public void onResume(){
super.onResume();
//dbHelper.open();
System.out.println("onResumr");
lv.setSelectionFromTop(index, top);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setTitle("Timeline");
uiHelper.onResume();
AppEventsLogger.activateApp(getActivity());
updateUI();
}
This also forces my AsyncTask to run again and again, which is an overhead.
Edit:
The root of this problem - After struggling for so many days I borrowed a friends phone to test and all was sorted on this new phone. I found out that I had turned on the Do not keep Activities option in my Developer Settings. The Dumb me!!
This is, unfortunately, the default behavior of the Fragment class. A Fragment is destroyed whenever the containing Activity is paused, and recreated whenever the containing Activity is resumed. If you use an Activity instead of a Fragment for the list, you would not experience the same behavior. With an Activity:
AsyncTasks and/or web services would not be called again.
The list would show the previously scrolled position.
If you want the same behavior with a Fragment, you need to override the onSaveInstanceState() method. And while interesting, it is not a small amount of work.
EDIT:
Make sure the Do not keep Activities option is unselected in your phone's Developer Settings. This, though, does not change the essential behavior of the Fragment class that I have outlined above.
You can call setRetainInstance(true) on your fragment. The lifecycle will be slightly different though.
A nice view of a fragment's lifecycle is available here
http://corner.squareup.com/2014/10/advocating-against-android-fragments.html
I have an application that has two fragments as actionbar tabs. The fragments are attached/detached when switching between the tabs. Any time I switch a tab, change the orientation, or press back to exit the application, the view is destroyed. I need it to be restored to its previous state when it is reopened. I know, at least on the orientation change, to use onSaveInstanceState and save the data there so I can restore it when the view is recreated. However, for some reason even though the data gets saved properly to the outState bundle and is read properly from the savedInstanceState bundle, the view doesn't update to what it should update to. For example, I start a service and while that service is running I need to hide two buttons and show two other buttons in their place. I use a boolean to check if the service is running, then put it in the outState so I can see which buttons to show or hide. My code for that is:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isRunning", isRunning);
}
In onCreateView:
if (savedInstanceState != null) {
isRunning = savedInstanceState.getBoolean("isRunning", false);
if (isRunning) {
showStopButton();
}
}
And the showStopButton code is:
private void showStopButton() {
btnStart.setVisibility(View.GONE);
btnReset.setVisibility(View.GONE);
btnStop.setVisibility(View.VISIBLE);
btnLoop.setVisibility(View.VISIBLE);
}
So all this works, the boolean is found as true while the service is running, and showStopButton() is called. However, it doesn't appear to actually do anything. The view state just resets itself to as if the first two buttons (which I want to be hidden) are shown instead of the ones I actually want to be shown. Any idea why this is happening/how to fix it?
I also have a listview that I need to stay populated with the same values as before that I can't get to work either.
Also, onSaveInstanceState isn't called when switching tabs (and I think not when pressing the back button either?). How should I go about retaining the view state in these cases?
I have a PreferencesActivity with a nested PreferenceScreen, so the PreferenceScreen has its own line in the list of preferences, and when you click on it, you're presented with a few more options.
When the user changes one of those options, I update that option's summary to show what the user has selected.
I also want to change the summary of the PreferenceScreen on the main list to show the values of its sub-settings. I'm using an OnSharedPreferenceChangeListener to update both summaries at once. However, only the summary of the preference gets changed, not the summary of the preference screen.
Here is simplified code:
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(SUB_PREFERENCE)){
updateSubPreferenceSummary(); //works
updatePreferenceScreenSummary(); //doesn't work
} else if (key.equals(SOME_OTHER_PREF_IN_ROOT_OF_PREFERENCEACTIVITY) {
updatePreferenceScreenSummary(); //works. (not what I want--just illustrating issue)
}
}
My theory is that Android is ignoring the first screen when it's not visible, so it doesn't get updated. I tried overriding onBackPressed(), but that doesn't work either.
I found a work-around. Apparently, the summary was getting updated, but the view from the parent screen was not getting invalidated/redrawn.
So I do this after updating the summary:
mHandler.post(new Runnable(){
public void run(){onContentChanged();
}});
I put it in the Handler just as a precaution, because I'm not sure what thread the onSharedPreferenceChanged() gets called from.