Here is a tab for 2 fragment, at the 1st fragment click button_save it will perform save data and stay at current page, but when I tab to 2nd page which is 2nd fragment, the data wont update then i need to close my application and open back to the 2nd page fragment then only the data being update, how can i solve this problem that i no need close my application when tab to 2nd page and getting auto update the data in 2nd page ? use Intent ?
the code below is from 1st fragment
public class KeyInWeightF extends Fragment implements View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contentView=inflater.inflate(R.layout.daily_weight_fragement, container, false);
btnSave = (Button) contentView.findViewById(R.id.button_save);
dbconnection = new SQLControlerWeight(getActivity());
dbconnection.openDatabase();
btnSave.setOnClickListener(this);
return contentView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button_save:
if (btnTime.getText().toString().equals("")||btnDate.getText().toString().equals("")
||kgnum.getText().toString().equals("")||bf.getText().toString().equals("")){
Toast.makeText(getActivity(), "Please insert all the detail!", Toast.LENGTH_LONG).show();
}
else{
String kgn = kgnum.getText().toString()+" kg";
String bodyfat = bf.getText().toString()+" %";
String date = btnDate.getText().toString();
String time = btnTime.getText().toString();
String comment = comm.getText().toString();
dbconnection.insertNote(kgn, bodyfat, date, time, comment);
Toast.makeText(getActivity(), "Suggest", Toast.LENGTH_LONG).show();
// Did i need add some code "Intent "here ??
}
break;
}
}
}
This is the 2nd fragment
public class HistoryF extends Fragment implements YourFragmentInterface{
View contentView;
ListView list;
SQLControlerWeight dbconnection;
TextView weight_num, date_num, time_num, bf_num, comment,weight_ID;
private SimpleCursorAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contentView = inflater.inflate(R.layout.history_fragment, container, false);
dbconnection = new SQLControlerWeight(getActivity());
dbconnection.openDatabase();
list = (ListView) contentView.findViewById(R.id.listViewWeight);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
weight_ID = (TextView) view.findViewById(R.id.weight_id);
weight_num = (TextView) view.findViewById(R.id.weight_num);
bf_num = (TextView) view.findViewById(R.id.bf_num);
date_num = (TextView) view.findViewById(R.id.date_num);
time_num = (TextView) view.findViewById(R.id.time_num);
comment = (TextView) view.findViewById(R.id.comment_text);
String weightId = weight_ID.getText().toString();
String wn = weight_num.getText().toString();
String bfn = bf_num.getText().toString();
String dn = date_num.getText().toString();
String tn = time_num.getText().toString();
String cm = comment.getText().toString();
Intent modify_intent = new Intent(getActivity(), Update_detail_info.class);
modify_intent.putExtra("weightId", weightId);
modify_intent.putExtra("dateNum", dn);
modify_intent.putExtra("timeNum", tn);
modify_intent.putExtra("weightNum", wn);
modify_intent.putExtra("bodyFatNum", bfn);
modify_intent.putExtra("comment", cm);
startActivity(modify_intent);
}
});
return contentView;
}
@Override
public void fragmentBecameVisible() {
Cursor cursor = dbconnection.readNote();
String[] from = new String[]{
DBHelperNote.WEIGHT_ID,
DBHelperNote.WEIGHT_NUM,
DBHelperNote.BODY_FAT,
DBHelperNote.WEIGHT_DATE,
DBHelperNote.WEIGHT_TIME,
DBHelperNote.WEIGHT_COMMENTS
};
int[] to = new int[]{
R.id.weight_id,
R.id.weight_num,
R.id.bf_num,
R.id.date_num,
R.id.time_num,
R.id.comment_text
};
adapter = new SimpleCursorAdapter(
contentView.getContext(), R.layout.history, cursor, from, to,0);
adapter.notifyDataSetChanged();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
this is my main activity :
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new KeyInWeightF(), "TRACK");
adapter.addFragment(new HistoryF(), "HISTORY");
adapter.addFragment(new AnalysisF(), "GRAPH");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Interface:
public interface YourFragmentInterface {
void fragmentBecameVisible();
}