I have a couple text views in a nested fragment that updates as you touch different objects. When I first enter the fragment it works then when I replace the fragment and reopen it. All the views are click able but my text views are not updating. If I do
((TextView)findViewById(R.id.txt_subtitle)).getText()
it returns the updated value as if it was working but the screen is not getting updated.
Its like i have an invisible copy of the text view some where
Unfortunately I can't post the source.
I'm not sure if this would work, since you haven't shown the code, but maybe you can override the onPause() method in the fragment to make the MainActivity class store the TextView information. Then, when you replace the next fragment with the fragment that's giving you the issue, you can set the TextViews to the data that was stored in the MainActivity. Does that make any sense?
EDIT: it might be the onDestroy() method if you don't invoke addToBackStack()
Related
I am using a recycler view to show some items, everything works fine but when I open another fragment and then reopen the fragment having recyclerview. it doesn't show anything.
Can somebody tell me what is its cause?
You should have a look at the lifecycle of a fragment.
It seems your code for filling the recyclerview with data is in the wrong method.
Lifecycle methods like onCreate(), onCreateView() etc. are just called on creating the fragment.
if you leave the fragment (and do not finish() it by yourself), it won't be destroyed so these methods are not called again.
You need to fill your recycler view in onResume() method
There are some posts here related to this issue, but I am trying to figure out what is going on in my particular case. Basically I have an Activity that instantiates a ListFragment to display a list of items that the user can scroll. When a list item is selected, I launch a separate Activity that "takes over" the screen to display details of the selection.
The issue is that when the Back button is pressed in this 2nd activity, the ListFragment's list is again displayed, but it's always repositioned to the top of the list, and I can't figure out how to correctly get the previous state of the list so that I could reposition it with setSelection, etc.
I do have a saveInstanceState in the fragment and it is being called when the details activity is launched, just as I would expect. However, I can't see any obvious fragment method that will be called to receive this Bundle back when the details activity returns. onCreateView and onViewCreated are not called again, and I can't use onViewStateRestored since I'm targeting API 14 (if that's even applicable).
Any ideas? This has to be something pretty basic that I'm missing!
The call to the deprecated startManagingCursor was the cause. I removed it and everything works fine. There was no need to get into a CursorLoader since I am using a simple CursorAdapter with a local SQLite database.
I'm having difficulty understanding the lifecycle of my fragment when the same fragment is created across three tabs. The main widget in each fragment is an EditText object that that is tracked in a class I created, one per tab, created when my app first executes. When the app executes the input (which is provided by buttons on the screen, not a keyboard) sets text into the second tab's EditText.
The input appears to be following getItem() inside my FragmentPagerAdapter. I understand that getItem() is called twice when my tabs are created and that this is perfectly normal behavior but what I'm not understanding is if this is a focus issue or a logic issue with my code design (I'm hoping the former). Could it have to do with instantiate() being returned by getItem()? Once instantiate() is called, does focus always follow it since I have this line of code in the onCreateView() of my fragment?
active.editLine = (EditText) v.findViewById(R.id.display);
// active is the currently active object of my app's class.
The reason I suspect instantiate() is causing my error is because when I increased the rendering of pages via setOffscreenPageLimit(2) the text was being sent to the third tab.
At this point I tried to track what was happening where and it was a nice educational exercise. What I found in my struggles was this is probably a focus issue-- however my attempts at clearing focus and setting focus did not appear to be applied, nor am I even sure if that's the underlying issue. I ended up using active.editLine.debug(0) to see if focus was being lost or gained and it wasn't. I read some of this fairly popular post and took my focus out of my XML but that didn't help either.
Just to make sure, where should I be setting my EditText objects? In OnCreateView() in my fragment? That is where the above code currently lives. I create three objects of my custom class in my activity as global variables. I have getters and setters in what I feel are appropriate areas of my code. Should the above code instead live in onCreate() of my fragment? If you think this is a focus issue, what is the accepted method of turning off focus and only using it when I need it? More importantly, when my app first runs, where do I turn on focus? OnTabSelected() is called before getItem() runs twice to render the first two tabs. So where would I gain initial focus if I'm globally turning it off? Can set focus with a type of listener?
Not exactly sure what you're going for here, but here are some suggestions:
have your global active object live in the main activity and call getActivity().someObject from your fragments
findViewById() will search down the view hierarchy until it finds the first instance of R.id.display
your fragment views won't be available until you commit them with a fragment transaction, this means that their views also won't be available until that time
you are "scoping" findViewById() to some view v by calling v.findViewById()
if you are paging through fragments, perhaps try setting your active view in the fragment's onResume() method
the fragment's onCreateView() method is only called once. This will be called between onCreate(Bundle) and onActivityCreated(Bundle).
I am trying to update my listfragment when a button is pressed. the button puts a name into a database but how do I update the list so that name appears? the listfragment uses a cursorLoader to load from the database. I even tried using a content observer on the database and calling getLoaderManager().restartLoader() but that didnt work either.
So what can I do to update the list when I click a button in an activity? I have found very little information on this please help
I can't be sure, but I assume there is something weird going on with the interaction between your Fragment and your Activity, which you are apparently displaying simultaneously somehow. Is there any reason why you are listening for the Button in the Activity? Are you dealing with a multi-pane screen or something? It seems like it'd be a lot simpler to just have the Fragment deal with this itself (since it is the one implementing the LoaderCallbacks, I assume).
Also, be sure that you are implementing your Fragment lifecycle correctly... i.e. you call initLoader() and set the list's adapter in onActivityCreated() as opposed to onCreate().
Hope that helps!
I really,really need a serious help over here.
I am creating an android app which uses PagerAdapter to create Fragments in an activity. Different Fragment consists of different views according to need which are created during run time. In the last fragment, I have created a sort of "Submit button", which when clicked, is supposed to get user entered values from each views(like EditText) from all Fragments.
I am using following command to get the views(int the above mentioned button clicklistener):
EditText e = (EditText) (getActivity().findViewById(i));
But its not geting the view with that ID except of last two fragments.
So, I am assuming that, it is saving the state of only last two fragments in that activity.
So, How can I save the 'view states' of all the fragments in the activity??
And Isn't it so that, when a view is created in a Fragment, and is added in its layout , that view is also added in the main activity layout?? Am I understanding it in the wrong way??Please let me know.
To simplify it, my question is:
How can we save the contents, entered by users, in dynamically created EditText in Fragments, created using the ViewPager (So that it can be accessed later)??
When you add a fragment to a FragmentPagerAdapter (assuming you haven't written a custom pager) it is added to the FragmentManager. The Fragment manager is independent of the activity lifecycle so it retains the fragment state.
If you take a look here: http://code.google.com/p/ratebeerforandroid/source/browse/trunk/JakeWharton-ActionBarSherlock/library/src/android/support/v4/app/FragmentPagerAdapter.java?spec=svn42&r=42
You can get an idea of how it works. If you want to save the fragment state for another session you'll need to pull back each fragment (getItem) and either use the onSaveInstanceState or write your own custom function
Hope that helps