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 startActivityForResult(or broadcast/receiver) to get the result from the activity. Then use adapter. notifyDataSetChanged to refresh the fragment.
OnPageChangeListener is the correct way to go, but you will need to refactor your adapter a bit in order to keep a reference to each Fragment contained in the FragmentPagerAdapter. Then, instead of creating a new Fragment, use the one contained in the adapter: mViewPager. addOnPageChangeListener(new ViewPager.
You can access another Fragment by its tag: // find your fragment YourFragment f = (YourFragment) getSupportFragmentManager(). findFragmentByTag("yourFragTag"); // update the list view f. updateListView();
when your activity is recreated, the fragment gets destroyed. So you have to create new instance of fragment and add it again. Ok, but you cant retain the fragment view. If you want to retain the data which is in fragment, then use onSaveInstanceState() and onRestoreInstanceState() .
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 FragmentStatePagerAdapter instead of FragmentPagerAdapter
You can try like this:
private static FragmentStatePagerAdapter adapter ;
private static String[] TAB_NAMES = null;
@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 FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return HomeFragment.getInstance();
case 1:
return new ProfileFragment();
default:
return HomeFragment.getInstance();
}
}
@Override
public int getCount() {
return TAB_NAMES.length;
}
@Override
public int getItemPosition(Object fragItem) {
int position = 0;
if (fragItem instanceof HomeFragment) {
position = 0;
} else if (fragItem instanceof ProfileFragment) {
position = 1;
}
return (position >= 0) ? position : POSITION_NONE;
}
}
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
indicator.setViewPager(pager);
}
Reference
You can check this solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With