I have a MainActivity
and a HomeTabs
with three tabs(A B C) all of them are Fragment
. I set a onRefresh
on tab C. When I trigger onRefresh
that its view is gone. Why is that? I couldn't solve it.
Any help would be appreciated. Thanks in advance.
Here's my HomeTabs
:
public class HomeTabs extends Fragment {
private Context context;
private TabLayout tabLayout;
private int[] imageResId = {R.drawable.input_icon_color,
R.drawable.note_icon_color,
R.drawable.analytics_icon_color};
public HomeTabs() {
// Required empty public constructor
}
public static HomeTabs newInstance() {
return new HomeTabs();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_tabs, container, false);
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab1)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab2)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab3)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPagerDataReport);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.getTabAt(0).setIcon(R.drawable.input_icon);
tabLayout.getTabAt(1).setIcon(imageResId[1]);
tabLayout.getTabAt(2).setIcon(imageResId[2]);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());/
int id = tab.getPosition();
if (id == 0) {
tabLayout.getTabAt(0).setIcon(R.drawable.input_icon);
tabLayout.getTabAt(1).setIcon(imageResId[1]);
tabLayout.getTabAt(2).setIcon(imageResId[2]);
}
if (id == 1) {
tabLayout.getTabAt(0).setIcon(imageResId[0]);
tabLayout.getTabAt(1).setIcon(R.drawable.note_icon);
tabLayout.getTabAt(2).setIcon(imageResId[2]);
}
if (id == 2) {
tabLayout.getTabAt(0).setIcon(imageResId[0]);
tabLayout.getTabAt(1).setIcon(imageResId[1]);
tabLayout.getTabAt(2).setIcon(R.drawable.analytics_icon);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
// My tab C position is 2 , when i refresh that i want to show tab C
if (getActivity().getIntent().hasExtra("refresh")){
tabLayout.getTabAt(2).select();
}
return view;
}
}
Here is my tab C Fragment
about refresh:
@Override
public void onRefresh() {
refreshLayout.setRefreshing(false);
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.refreshFragment();
}
And the MainActivity
:
public class MainActivity extends AppCompatActivity {
private FrameLayout frameLayout;
private Toolbar toolBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolBar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolBar);
getSupportActionBar().setTitle("");
toolBar.setTitle(R.string.myAppName);
toolBar.setNavigationIcon(R.drawable.medical_history32);
toolBar.setTitleTextColor(Color.WHITE);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
if (savedInstanceState == null) {
switchFragment(HomeTabs.newInstance());
}
}
public void switchFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameLayout, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
new DialogHandler(this).quickDialog(
getResources().getString(R.string.quick),
getResources().getString(R.string.confirm),
getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
return super.onKeyDown(keyCode, event);
}
public void refreshFragment() {
//switchFragment(HomeTabs.newInstance());
Intent intent = new Intent(this,MainActivity.class);
intent.putExtra("refresh",true);
startActivity(intent);
}
}
Here's my tab C:
public class MyLineChart extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_my_line_chart, container, false);
//................set my data
return view;
}
@Override
public void onRefresh() {
refreshLayout.setRefreshing(false);
}
There are many ways to do it.
If you are using ViewPager
the best solution is found here.
You must implement (override) getItemPosition
method in your page adapter to return (no position changed)
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
Then to update from anywhere, you just notify the adapter of a change
mPagerAdapter.notifyDataSetChanged();
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