我有两个片段活动。现在我想在此之间切换。我也希望在第一个片段活动结束后执行此操作。
以下是我的片段活动类
MainActivity.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_buyer_home_page, container, false);
return view;
}
SecondActivity.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class MainActivity extends FragmentActivity {
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int page=2;
pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
pager.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (pager.getCurrentItem()==page) {
return true;
}
return false;
}
});
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("FirstFragment");
case 1: return SecondFragment.newInstance("SecondFragment");
case 2: return ThirdFragment.newInstance("ThirdFragment");
case 3: return FourthFragment.newInstance("FourthFragment");
case 4: return FifthFragment.newInstance("FifthFragment");
default: return ThirdFragment.newInstance("DefaultFragment");
}
}
@Override
public int getCount() {
return 5;
}
}
public void setCurrentItem(int which) {
if(pager != null && which >= 0 && which <= 4) {
pager.setCurrentItem(which);
}
}
}
问题我该怎么做?
答案 0 :(得分:0)
尝试覆盖FirstActivity的第五个片段(fifthFragment.java)中的OnDetach()方法,并在ondetach()方法内调用getActivity()。startActivity(secondActivity);