需要从不同的活动而不是默认的第一个选项卡

需要从不同的活动而不是默认的第一个选项卡

问题描述:

我在TabLayout中创建了片段选项卡。我点击了我的FloatingActionButton,它将我带入一个空的活动,然后在该空活动中有一个“后退”按钮,我希望该后退按钮可以将我带到当我单击FloatingActionButton时所在的选项卡上。需要从不同的活动而不是默认的第一个选项卡

现在,我所得到的只是返回到默认的第一个标签,不管我在3个标签中选择哪一个。

public class MainActivity extends AppCompatActivity { 

private SectionsPagerAdapter mSectionsPagerAdapter; 


private ViewPager mViewPager; 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    // Create the adapter that will return a fragment for each of the three 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    //setup the viewPager adapter 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 



    final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 


      Intent startIntent = new Intent(getApplicationContext(), BroadcastPage.class); 
      //Go To Log In XML File 
      startActivity(startIntent); 

      Intent welcome = new Intent(MainActivity.this, BroadcastPage.class); 


      finish(); 

     } 
    }); 


} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(menu_main, menu); 
    return true; 
} 



@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 

     // launch settings activity 
     startActivity(new Intent(MainActivity.this, SettingsActivity.class)); 


     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 



    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch(position) { 


    case 0: 
    World activity_world_tab = new Worldtab(); 
    return activity_world_tab; 


    case 1: 
    BroadcastTab activity_broadcast_tab = new BroadcastTab(); 
    return activity_broadcast_tab; 



case 2: 
    GroupsTabBar activity_groups_tabbar = new GroupsTabBar(); 
    return activity_groups_tabbar; 


      default: 
       return null; 

     } 

    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 



    @Override 
    public CharSequence getPageTitle(int position) { 



     switch (position) { 
      case 0: 
       return ""; 
      case 1: 
       return "SECTION 2"; 
      case 2: 
       return "SECTION 3"; 
     } 
     return null; 

     } 
     } 
     } 

你需要删除你的工厂按钮的点击收听完成()。这应该可以解决你的问题。这就是你的晶圆厂按钮应该看起来像onClickListener:

fab.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 


     Intent startIntent = new Intent(getApplicationContext(), BroadcastPage.class); 
     //Go To Log In XML File 
     startActivity(startIntent); 

     Intent welcome = new Intent(MainActivity.this, BroadcastPage.class); //you could erase this line since its not really needed 

    } 
}); 
+0

谢谢我试过了,但它不能解决我的问题。它并没有改变任何东西 – iBEK

+0

我认为你的onRestoreInstanceState(Bundle savedInstanceState)函数的条件存在一些问题。 savedInstanceState.getInt(“PAGE 0”)应该替换为savedInstanceState.getInt(“PAGE”) –

+0

所有与保存和恢复实例状态关联的代码可能有问题 – iBEK