我想用activity
fragments
Intent intent = new Intent(NewMealActivity.this, MainActivity.class);
intent.putExtra("id", 1);
startActivity(intent);
MainActivity
包括三个fragments
(Frag1,Frag2,Frag3)。
我想打开Frag3,但打开Frag1
这是我的MainActivity,有三个标签
public class MainActivity extends FragmentActivity {
private String txtFragmentMyData;
private String txtFragmentProductList;
private String txtFragmentCalorieCalculator;
private TabLayout tabLayout;
private long tabId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtFragmentMyData = getString(R.string.txt_fragment_my_data);
txtFragmentProductList = getString(R.string.txt_fragment_product_list);
txtFragmentCalorieCalculator = getString(R.string.txt_fragment_calorie_calculator);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentMyData));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentProductList));
tabLayout.addTab(tabLayout.newTab().setText(txtFragmentCalorieCalculator));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
final ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
Bundle extras = getIntent().getExtras();
if (extras != null) {
tabId = extras.getLong("id");
}
if (tabId > 0){
viewPager.setCurrentItem(2);
} else {
viewPager.setCurrentItem(0);
}
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
我想,在第一次开始时会打开第一个标签,但是如果我点击其他活动中的按钮会打开第三个标签