0

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; 

     } 
     } 
     } 

답변

0

당신은 당신의 팹 버튼의 클릭 리스너에 마무리()를 제거해야합니다. 그게 너의 문제를 해결해야 해. 이것이 바로 fab 버튼에 대한 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

관련 문제