我对带有片段的浏览器有点失望,因为 4 天我尝试了很多解决方案,但没有成功。
我是片段的初学者,我按照一个很棒的教程来创建我的浏览器。我可以旋转手机碎片没有问题。但是我必须从 viewpager 中的片段打开一个新活动,然后返回包含 viewpager 的主机活动。在这一点上,当我返回活动 B 时,我无法从主机活动中获取我的片段。我不明白我能做什么?
例子:
TabSessionActivity --> Fragment A --> Activity B --> TabsessionActivity(fragment A =NULL)
TabSessionActivity --> Fragment A --> 旋转手机 --> TabsessionActivity(fragment A OK!)
这里我的适配器:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments;
private FragmentManager mFragmentManager;
// On fournit à l'adapter la liste des fragments à afficher
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragmentManager=fm;
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
for(Fragment mfragment:fragments){
Log.i("Fragment Tag and Id"," FragmentTag:"+mfragment.getTag()+" FragmentID:"+mfragment.getId()+"NB fragments:"+this.fragments.size());
}
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
public Fragment getActiveFragment(ViewPager container, int position) {
String name = makeFragmentName(container.getId(), position);
for(Fragment mfragment:mFragmentManager.getFragments()){
Log.i("Fragment Tag and Id"," FragmentTag:"+mfragment.getTag()+" FragmentID:"+mfragment.getId()+" NB fragments:"+this.mFragmentManager.getFragments().size());
}
return mFragmentManager.findFragmentByTag(name);
}
private static String makeFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
}
然后是我的主机活动:
public class TabsSessionActivity extends SherlockFragmentActivity {
private GeoLocatingService mGeoService;
private MyPagerAdapter mPagerAdapter;
private ViewPager mViewPager;
// private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
private Dashboard_Fragment fragment1;
private OsmMapFragment fragment2;
private Fragment1 fragment3;
final IGeoLocatingServiceListener servicelistener = new IGeoLocatingServiceListener() {
@Override
public void dataChanged(final Object data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i("TabSessionActivity:GeoService","Service rafraichi");
refreshCurrentFragment();
}
});
}
};
/************************** SERVICE CONNECTION **********************/
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("GeoService", "TabsSessionActivity:Disconnected!");
mGeoService = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
Log.i("TabSessionActivity:GeoService", "TabsSessionActivity:Connected!");
mGeoService = ((GeoLocatingService.LocalBinder) service).getService();
mGeoService.addListener(servicelistener);
Log.i("TabSessionActivity:GeoService","Service connecté");
refreshCurrentFragment();
}
};
public GeoLocatingService getGeoLocatingService() {
return mGeoService;
}
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the view from main_fragment.xml
setContentView(R.layout.activity_tabs_session);
// Création de la liste de Fragments que fera défiler le PagerAdapter
List fragments = new Vector();
// Ajout des Fragments dans la liste
fragments.add(Fragment.instantiate(this,OsmMapFragment.class.getName()));
fragments.add(Fragment.instantiate(this,Dashboard_Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Fragment1.class.getName()));
// Création de l'adapter qui s'occupera de l'affichage de la liste de
// Fragments
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(),fragments);
// Affectation de l'adapter au ViewPager
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(this.mPagerAdapter);
//Bind the title indicator to the adapter
LinePageIndicator LineIndicator = (LinePageIndicator)findViewById(R.id.indicator);
LineIndicator.setViewPager(mViewPager);
//Style Line Indicator
final float density = getResources().getDisplayMetrics().density;
LineIndicator.setSelectedColor(0xFF45A5D7);
LineIndicator.setUnselectedColor(0xFF888888);
LineIndicator.setStrokeWidth(4 * density);
LineIndicator.setLineWidth(30 * density);
mViewPager.setCurrentItem(1);
Log.i("TabSessionActivity:GeoService","On Create");
}
public void refreshCurrentFragment(){
//Retrieve fragments instances
fragment1 = (Dashboard_Fragment) this.mPagerAdapter.getActiveFragment(mViewPager, 1);
fragment2 = (OsmMapFragment) this.mPagerAdapter.getActiveFragment(mViewPager, 0);
fragment3 = (Fragment1)this.mPagerAdapter.getActiveFragment(mViewPager, 2);
//Check if fragment is active and visible
if (fragment1!=null){
Log.i("TabSessionActivity:GeoService","Fragment trouvé");
fragment1.onDatachangedCallback();
}
if (fragment2!=null){
fragment2.onDatachangedCallback();
}
if (fragment3!=null){
fragment3.onDatachangedCallback();
}
}
@Override
public void onStart() {
super.onStart();
//On attache la vue au service
bindService(
new Intent(this, GeoLocatingService.class),
connection, BIND_AUTO_CREATE);
Log.i("TabSessionActivity:GeoService","On start bind");
}
@Override
public void onResume() {
super.onResume();
if(mGeoService!=null){
Log.i("TabSessionActivity:GeoService","RefreshCurrentFragment()");
}
}
@Override
public void onPause() {
super.onPause();
if(mGeoService!=null){
unbindService(connection);
mGeoService.removeListener(servicelistener);
mGeoService=null;
Log.i("TabSessionActivity:GeoService","On pause unbind");
}
}
@Override
public void onDestroy() {
if(mGeoService!=null){
unbindService(connection);
Log.i("TabSessionActivity:GeoService","On destroy unbind");
}
/*if(ServiceTools.isGeoLocatingServiceRunning(this)){
stopService(new Intent(this, GeoLocatingService.class));
}*/
super.onDestroy();
}
@Override
public boolean onKeyDown(int keycode, KeyEvent event) {
if (keycode == KeyEvent.KEYCODE_BACK) {
if(mGeoService!=null){
if(mGeoService.getRecord()){
showCancelRecordAlertToUser();
}else{
startActivity(new Intent(this, MainActivity.class));
finish();
}
}else{
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
return super.onKeyDown(keycode, event);
}
当我从活动 B 返回主机活动时,Log Cat 对片段说 null :
fragment1 = (Dashboard_Fragment) this.mPagerAdapter.getActiveFragment(mViewPager, 1);
fragment2 = (OsmMapFragment) this.mPagerAdapter.getActiveFragment(mViewPager, 0);
fragment3 = (Fragment1)this.mPagerAdapter.getActiveFragment(mViewPager, 2);