Reputation: 35
Hello I am working on a android app that has a view pager and three fragments in it by default the app starts on fragment 1 but I would like it to start on fragment 2 instead. How do i do this in android??
heres my viewpager code:
package com.learn2crack.tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabPagerAdapter extends FragmentPagerAdapter {
// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg) {
switch (arg) {
case 0:
//Fragement for Calculator
return new FragmentTab1();
case 1:
//Fragment for Calender
return new FragmentTab2();
case 2:
//Fragment for Phone
return new FragmentTab3();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
and if you need it heres my main activity:
package com.learn2crack.tab;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import com.d4a.tobias1.R;
import com.learn2crack.pager.Launchalot;
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setTabListener(tabListener));
}
/** Called when the user clicks the apps button */
public void apps(View view) {
Intent myIntent1 = new Intent(MainActivity.this, Launchalot.class);
// Intent myIntent1 = new Intent(MainActivity.this, Main.class);
MainActivity.this.startActivity(myIntent1);
}
/** Called when the user clicks the notes button */
public void notes(View view) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.d4a.notes","com.example.note.NoteList"));
intent.putExtra("grace", "Hi");
startActivity(intent);
}
/** Called when the user clicks the play button */
public void play(View view) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.android.vending");
startActivity(launchIntent);
}
/** Called when the user clicks the web button */
public void web(View view) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.d4a.pegasus","acr.browser.barebones.activities.BrowserActivity"));
intent.putExtra("grace", "Hi");
startActivity(intent);
}
/** Called when the user clicks the email button */
public void email(View view) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.d4a.eMailTime","com.fsck.k9.activity.Accounts"));
intent.putExtra("grace", "Hi");
startActivity(intent);
}
/** Called when the user clicks the sms button */
public void chat(View view) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.d4a.sms","de.ub0r.android.smsdroid.ConversationListActivity"));
intent.putExtra("grace", "Hi");
startActivity(intent);
}
}
any help would be amazing
Thanks in advance!!
Upvotes: 3
Views: 385
Reputation: 479
Have you tried calling tab.setCurrentItem(1); at the end of onCreate()?
Upvotes: 3