我在Activity TabLayout中有3个片段。第一个选项卡片段用于从用户获取数据并保存在数据库中。第2和第3个片段用于在ListView中显示此数据库数据。当有人在第一个片段中输入数据时,我必须立即在第二个和第三个片段中显示数据。所以我必须让我的片段一直刷新。我通过调用onResume()方法中的每个片段来刷新OnPageSeceted方法中的这个片段数据。我的ViewPagerAdapter类扩展了FragmentStatePagerAdapter。问题是当我在ViewPagerAdapter中扩展FragmentStatePagerAdapter时,我的第二个片段onResume仅在应用启动时调用frist time。但第三个活动onResume每次都在调用。因此,我的第二个片段未显示更新的数据库数据。只有第3个活动正确显示新数据。但有趣的是,当我通过FragmentPagerAdpater扩展ViewPagerAdapter onResume在我更改片段时正确调用。为什么FragmentStatePagerAdpater阻止仅为第二个片段调用onResume()?
注意: When my phone goes to sleep then after unlocking phone 2nd fragment calling onResume().
Project github链接:https://github.com/Forlonsunny/OnResumeInFragmentWithDataBase
SimpeTabActivity:
package com.theoaktroop.onresumeinfragmentwithdatabase;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.theoaktroop.onresumeinfragmentwithdatabase.fragments.QuestionnaireFragment;
import com.theoaktroop.onresumeinfragmentwithdatabase.fragments.SavedFormsFragment;
import com.theoaktroop.onresumeinfragmentwithdatabase.fragments.WelcomeFragment;
public class SimpleTabsActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
ViewPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(final ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new WelcomeFragment(), "Welcome");
adapter.addFragment(new QuestionnaireFragment(), "Question Fragment");
adapter.addFragment(new SavedFormsFragment(), "Answer Fragment");
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Fragment fragment = ((ViewPagerAdapter) viewPager.getAdapter()).getFragment(position);
if ((position == 1 || position == 2) & fragment!=null) {
System.out.println("Form Tabs position= "+position);
fragment.onResume();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
ViewPagerAdapter:
package com.theoaktroop.onresumeinfragmentwithdatabase;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by yeahi on 10/26/2015.
*/
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
//Added
private Map<Integer,String> fragmentTag;
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
//Added
private FragmentManager fragmentManager;
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
fragmentManager = manager;
fragmentTag = new HashMap<Integer,String>();
}
@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);
}
//Added
@Override
public Object instantiateItem(ViewGroup container, int position) {
Object obj = super.instantiateItem(container, position);
if(obj instanceof Fragment){
Fragment fragment = (Fragment) obj;
String tag = fragment.getTag();
fragmentTag.put(position,tag);
}
return obj;
}
//Added
public Fragment getFragment(int position){
String tag = fragmentTag.get(position);
if(tag == null){
return null;
}
return fragmentManager.findFragmentByTag(tag);
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
第二段:
package com.theoaktroop.onresumeinfragmentwithdatabase.fragments;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.theoaktroop.onresumeinfragmentwithdatabase.AnswerTabel.AnswerListAdapter;
import com.theoaktroop.onresumeinfragmentwithdatabase.AnswerTabel.AnswerModule;
import com.theoaktroop.onresumeinfragmentwithdatabase.AnswerTabel.AnswerTableDataBaseQuery;
import com.theoaktroop.onresumeinfragmentwithdatabase.R;
import java.sql.SQLException;
import java.util.List;
public class QuestionnaireFragment extends Fragment implements AdapterView.OnItemLongClickListener {
private ListView mListView;
private List<AnswerModule> mAnswerModuleList;
public static AnswerListAdapter mAdapter2;
private AnswerTableDataBaseQuery mAnswerTableDataBaseQuery;
private Context context;
private View rootView;
long ePID = 0;
public QuestionnaireFragment() {
// Required empty public constructor
}
@Override
public void onResume() {
Log.i("bug", " Question i am here dude");
context = getActivity();
mListView = (ListView) rootView.findViewById(R.id.listview_saved_form);
mAnswerTableDataBaseQuery = new AnswerTableDataBaseQuery(context);
mAnswerModuleList = mAnswerTableDataBaseQuery.getAllAnswerSavedQuestion();
mAdapter2 = new AnswerListAdapter(context, mAnswerModuleList);
// Log.d("bug", "here");
mListView.setAdapter(mAdapter2);
mListView.setOnItemLongClickListener(this);
super.onResume();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_savedforms, container, false);
return rootView;
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ePID = id;
showDeleteDialog();
return true;
}
private void showDeleteDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Delete Entry?")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
try {
mAnswerTableDataBaseQuery.answerTableDeleteByAnswerId(ePID);
} catch (SQLException e) {
e.printStackTrace();
}
forRefresh();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
public void forRefresh() {
mAnswerTableDataBaseQuery = new AnswerTableDataBaseQuery(context);
mAnswerModuleList = mAnswerTableDataBaseQuery.getAllAnswerSavedQuestion();
mAdapter2 = new AnswerListAdapter(context, mAnswerModuleList);
mListView.setAdapter(mAdapter2);
}
}
第3个片段与第2个片段相同。