android tabs - starting a new activity

前端 未结 5 603
迷失自我
迷失自我 2021-01-13 22:14

There are 4 Tabs in a TabHost, let them be A, B, C, and D. Now each one is just an index page and clicking on any of them shows a different activity.

The problem is

相关标签:
5条回答
  • 2021-01-13 22:53

    Try this, found this solution in android cookbook, http://androidcookbook.com/Recipe.seam;jsessionid=5424397F3130CE7769FF47DD67742911?recipeId=1693&recipeFrom=ViewTOC

    0 讨论(0)
  • 2021-01-13 23:03

    Can't you change the contentView of your tab instead of starting a new Activity ?

    Maybe I'm wrong but I think also that starting an activity in a tab isn't possible because the TabView is hosted in a activity and not the opposite (Tabview don't host an activity per Tab).

    0 讨论(0)
  • 2021-01-13 23:04

    It looses the view hierarchy. When you press the back button, in my case, the app closes.

    0 讨论(0)
  • 2021-01-13 23:06

    I think the common consensus is that it is best not to use individual Activities as tab content due to these limitations. See these questions and answers for pointers to alternatives:

    Android: Why shouldn't I use activities inside tabs? Android - Tabs, MapView, activities within tabs

    0 讨论(0)
  • 2021-01-13 23:14

    To summarize the link that Rukmal Dias provided. Here's what you do:

    1. Change your current Activity (that's in a tab) to derive from ActivityGroup
    2. Create a new intent for the Activity you want to switch to
    3. Copy/Paste and call this function in your current activity where "id" is the "android:id" for the layout of the new activity you want to switch to

       public void replaceContentView(String id, Intent newIntent){
            View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView(); 
            this.setContentView(view);}
      

    Here's an example of how I make the call to switch views from my current Tabbed Activity:

    public void switchToNextActivity(View view)
    {
        Intent myIntent = new Intent(getApplicationContext(), MyNextActivity.class);
        replaceContentView("next_activity", myIntent);
    }
    
    0 讨论(0)
提交回复
热议问题