ViewPager - Update fragment from activity - android

I have a ViewPager with 3 fragments. One fragment downloads data ​from a website and displays it in TextViews. Now when the user clicks in the menu of the MainActivity on the item "refresh", the fragment should redownloads the data and displays it.
I tried to create a function refresh in the fragment's class and call it from the MainActivity's menu. That worked, but then it crashes because the app can't find the textview. The variable view is empty and it causes a null pointer exception.
What can I do to resolve this?
Many thanks!

From your fragmentactivity,
Fragment tehFraggerz = getSupportFragmentManager().findFragmentByTag(string);
where string is the tag. You set the tag of the fragments in the add, replace, etc methods.
Once you have your fragment, you can do
TextView tehTexterz = (TextView) tehFraggerz.getView().findViewById(R.id.tehText);

Related

what is the best way to update fragment view from activity

I have one fragment and that have some view element like textview buttons.
Now My problem is I have some service running background and it is checking data from server(Mysql) when these are some data it tells to Mainactivity.
Now on the basis of result I have to update fragment texeviews buttos.
In fact the fragment is a section of activity.
In the activity,you will get the fragment which involve.
You get the fragment ,so you can do you want to do .

How to refresh fragment from base fragment

Sorry for bad English.
I have BaseFragment. it contains Search View and View Pager and View pager has three fragement like Fragment A,B,C
Fragment B has Listview.
so whenever i am doing search i got search response in Base fragment now what i want to do is push these response to fragment B listview inside viewpager. when every time search action perform and need to refresh listview.
You can get an instance of the fragment you want by calling Fragment manager like this:
CustomFragment fragment = (CustomFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_a);
and then you can call any methods of the fragment like this:
fragment.update(data);
But make sure you check the fragment is not null before calling its methods.

How to call mypageradapter.notifyDataSetChanged() from another activity?

My app has the next structure, I have the Mainactivity where I have my viewPager. Inside this viewpager I add new fragment Activities. These Fragment Activities have a TextView with their names, I have created a OnlongClickListener in the textview so that when you OnlongClick in the textview the fragment Activity gets removed from the list in the viewpager I have in the main Activity.
I've tried to use in the fragment Activity the next code :
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
but the result is an empty space in the position where the deleted fragment was.
I think the solution would be calling mypageradapter.notifyDataSetChanged() in the fragment Activity but the thing is that the viewPager and the pagerAdapter are created in the mainActivity and I want to notify the changes in the fragment activity.
How can I call mypageradapter.notifyDataSetChanged() from the fragment activity?
In this moment I have a solution for this problem but I don't think this solution is the best one, when I delete the fragment I create an intent for the mainActivity and then I StartActivity(intent). In the mainActivity onStart method I call mypageradapter.notifyDataSetChanged() and then the empty spaces dissapear and the fragment list is sorted correctly. This solution is really slow and rough.
I would appreciate a better solution for my problem.
P.D: For help I have MyPagerAdapter class inside the mainActivity.

How to call a fragment layout from the FragmentActivity to avoid NullPointerException

I am using action bar tabs with FragmentPagerAdapter. (3 tabs)
I have onCreateOptions menu set at the MainActivity that extends FragmentActivity.
This menu contains a save button that runs saveData() method on the MainActivity, which saves the data in the whole 3 fragments.
The problem is that when I first open the app and hit the save button, I get NPE when this method tries to save data on the 3rd fragment. I know that this is because onCreateView() method is not called yet, and when I switch tabs, the saveData() method works fine.
The question is, how can I avoid the NPE? or how can I call the layout of the 3rd fragment so that saveData() can access its views.
Well, the simplest way would be to just check your Fragments views for null inside your saveData() method. If it is necessery to save data from every fragment, you can use this method on your ViewPager to force it to load all fragments at once.

How to restore Android fragment view state

I have application titles fragment and contents fragment on the screen. When the user clicks on the item in titles fragment the according fragment is created and inserted to a frame and the selected title is highlighted in titles fragment.
Transaction is done with fragment.addToBackStack(), so when the user clicks the BACK key, the previous fragment is restored and inserted in the frame.
What is the best solution to restore view state when transaction manager restores fragments?
The problem is that I should highlight previous fragment name in titles fragment and I should know what fragment it is. I resolved it by storing view state in my own stack: when fragment is created and restoring on changing transaction backstack using transaction manager listener.
But this doesn't seem like the correct solution.
Before the answer, next time remember to add your code. Chances are my answer will not help you as much as it could because I don't really know your code.
This is old but anyway, IF I understand your question (and app architecture) correctly, it sounds like a case for interfacing.
Example:
add this interface as a member to the content fragment:
public class ContentFragment extends Fragment{
public interface onFragmentTitleHighlighted{
public void highLightTitle(String title);
}
}
and have the title fragment implement it. Make sure to equip your content fragments with the title fragmnet, and add a call to highLightTitle(String title); in the content fragment's onCreateView(...) call. This way whenever a new content fragmnet is being added the title will be highlighted.

Categories

Resources