2017-12-18 292 views
0

我有一個選項卡片段應用程序。問題是當我進入我的應用程序內部的一個活動,並希望返回到我的標籤佈局它返回沒有選項卡行佈局。我只能看到特定的片段頁面,而沒有上面的行(選項卡的行),這使我能夠在選項卡之間導航。 你知道我能做些什麼來解決它嗎?我怎樣才能看到標籤的行呢? 感謝您的幫助,使用選項卡布局從活動中啓動選項卡片段

這是我希望看到的活動後面的第一個標籤:

public class Tab1MyProfile extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab1_my_profile, container, false); 
     return rootView; 
    } 
} 

這是包含代碼回到該片段選項卡活動:

public class GameLive extends AppCompatActivity implements RecognitionListener { 
    private Intent intent; 
    private Button mStopButton; 

    public void stopGame (View view) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Are you sure you want to finish the game?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         FragmentManager fragmentManager = getSupportFragmentManager(); 
         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
         Tab1MyProfile fragment = new Tab1MyProfile(); 
         fragmentTransaction.replace(android.R.id.content, fragment); 
         fragmentTransaction.commit(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.game_live); 
     mStopButton = (Button) findViewById(R.id.btnEndGame); 
     mStopButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       stopGame(view); 
      } 
     }); 

    } 

} 

這是一個包含所有選項卡的片段的活動:

public class StartActivity extends AppCompatActivity { 

    private SectionsPagerAdapter mSectionsPagerAdapter; 
    private ViewPager mViewPager; 

    @TargetApi(Build.VERSION_CODES.M) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_start); 

     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

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

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_start, 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) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 


    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

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

     @Override 
     public Fragment getItem(int position) { 
      switch (position) { 
       case 0: 
        Tab1MyProfile tab1=new Tab1MyProfile(); 
        return tab1; 
       case 1: 
        Tab2StartGame tab2=new Tab2StartGame(); 
        return tab2; 
       case 2: 
        Tab3StatsArea tab3=new Tab3StatsArea(); 
        return tab3; 
       case 3: 
        Tab4Settings tab4=new Tab4Settings(); 
        return tab4; 
      } 
      return null; 
     } 

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

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "My profile"; 
       case 1: 
        return "Start Game"; 
       case 2: 
        return "Stats Area"; 
       case 3: 
        return "Settings"; 
      } 
      return null; 
     } 
    } 


} 

再次感謝!

回答

0

如果將onClick方法的DialogInterface.OnClickListener替換爲GameLive.this.finish();,那該怎麼辦?這會在點擊時關閉GameLive活動,並返回到上一個活動,該活動應該是包含缺少導航欄的活動。

+0

這是一個好主意,但是我想要關閉的活動和包含缺少導航欄的片段之間存在活動。您是否知道如何一次關閉兩個活動? –

+0

您確定要一次關閉兩項活動嗎?如果您從用戶的角度思考,這可能會導致混淆。如果你真的想操縱活動堆棧,有辦法。例如,查看Intent標誌。 –

+0

謝謝你的幫助!我決定採取你的建議,關閉只有一個活動:) –

相關問題