I have got 2 tabs called tab1 (home) and tab2 (profile) using TabPageIndicator
and FragmentPagerAdapter
libraries.
In tab2, I have a button named "Log in" (tab2 inflates abc.xml
).
When users click "log in" button, the app will show a login form in a new activity.
After logging in, the activity finishes and the content view of tab2 will change ( it shows user's profile because it will inflate xyz.xml
). How can I do that?
public class MainActivity extends SherlockFragmentActivity {
private static String[] TAB_NAMES = null;
private Context context = this;
private static FragmentPagerAdapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (TAB_NAMES == null) {
TAB_NAMES = new String[] {
getResources().getString(R.string.tab_home),
getResources().getString(R.string.tab_profile) };
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new TabsAdapter(
getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater infalter = getSupportMenuInflater();
infalter.inflate(R.menu.main_menu, menu);
return true;
}
class TabsAdapter extends FragmentPagerAdapter {
public TabsAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return HomeFragment.getInstance();
case 1:
return new ProfileFragment();
default:
return HomeFragment.getInstance();
}
}
}
You can use FragmentStatePagerAdapter instead of FragmentPagerAdapter
You can try like this:
Reference
You can check this solution.
I was also facing the same issue, So i resolved it on my way:
Just refresh your HomeFragment or ProfileFragment by overriding onResume method.
In onResume Method you can refresh your ListFragment or Fragment easily.
You can use startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter.notifyDataSetChanged to refresh the fragment.
About refreshing the ViewPager, more detail:
ViewPager PagerAdapter not updating the View
I've tried a lot of solutions i found in google and stackoverflow about this problem. All of them did not work correctly. Also tutorials in official google android developer pages didn't work.
I implemented ViewPageIndicator to my application and tried to create an imageslider. I have dynamic urls for images comes from on service callback success. So i have to set my viewpager adapter and indicator in OnResume. When i set everything was ok i load fragments in onResume and after OnStop i reload them. But when i send app to background and open some other apps i realized after onResume my slider cant reload images.
After wasting my time, i tried to understand lifecyle of fragments which i created in fragmentpageradapter getItem clearly with logging and debugging onCreateView and onResume events.
If you create your fragments like
After you stop your activity and open some other applications, you loose your vars which you set on constructor of your custom fragment. When i realized this situation i tried bundle approach.
Here is my custom FragmentPagerAdapter (I used google's sample)
My custom FragmentPagerAdapter implements IconPagerAdapter. Be careful about that.
And my custom SliderFragment:
And finally my loadSlider method which loads slider items
I could not have chance to check if my slider fragments are being updated but at least fragments live after OnStop and thats what i needed most.