Lesson 77. Tab - Tabs. TabActivity. Activity as tab content

Lesson 77. Tab – Tabs. TabActivity. Activity as tab content


In this lesson:

– we use Activity as tab content
– we use TabActivity

You can use Activity tabs. To do this, there is an implementation of the setContent method, which accepts Intent input. And at Intent we prescribe what Activity we need.

There are nuances when using Intent and tabs. Our main Activity that contains TabHost is to NOT android.app.Activity as usual, but android.app.TabActivity. In this case, we don’t have to bother with additional initialization to work with Intent.

Let’s create a project:

Project name: P0771_TabIntent
Build Target: Android 2.3.3
Application name: TabIntent
Package name: en.startandroid.develop.p0771tabintent
Create Activity: MainActivity

Write the texts in strings.xml:



	TabIntent
	Это первое Activity
	Это второе Activity

screen main.xml:



	
		
			
			
			
			
		
	

You cannot change the id in TabHost when using android.app.TabActivity. Otherwise, the system simply won’t find TabHost.

FrameLayout is not filled because we will not use its components for the contents of the tabs. We will load the Activity goals there.

Let’s create an Activity pair.

layout files

one.xml:



	
	

two.xml:



	
	

classes

OneActivity.java:

package ru.startandroid.develop.p0771tabintent;

import android.app.Activity;
import android.os.Bundle;

public class OneActivity extends Activity {
  
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one);
  }
}

TwoActivity.java:

package ru.startandroid.develop.p0771tabintent;

import android.app.Activity;
import android.os.Bundle;

public class TwoActivity extends Activity {

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.two);
  }
}

Do not forget to spell them out in the manifesto.

Kodyma MainActivity.java:

package ru.startandroid.develop.p0771tabintent;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // получаем TabHost
        TabHost tabHost = getTabHost();
        
        // инициализация была выполнена в getTabHost
        // метод setup вызывать не нужно
        
        TabHost.TabSpec tabSpec;
        
        tabSpec = tabHost.newTabSpec("tag1");
        tabSpec.setIndicator("Вкладка 1");
        tabSpec.setContent(new Intent(this, OneActivity.class));
        tabHost.addTab(tabSpec);
        
        tabSpec = tabHost.newTabSpec("tag2");
        tabSpec.setIndicator("Вкладка 2");
        tabSpec.setContent(new Intent(this, TwoActivity.class));
        tabHost.addTab(tabSpec);
    }
}

Our Activity inherits TabActivity. This gives us the ability to get TabHost using the getTabHost method. We do not need to search for it ourselves on screen. Also, this wonderful method performs the usual initialization for us, we do not need to call the setup method like in the previous lesson. And besides the usual initialization, this method prepares to work with Activity as the contents of tabs.

And then you are familiar with everything from the last lesson. We create tabs, specify names. In the setContent method, instead of content from FrameLayout, we give Intent, which indicates the Activity we need.

We all save and launch the application.

The first tab of OneActivity

Second tab of TwoActivity

And how do Activity LifeCycle events work? When you first see the first tab, three methods work OneActivity: onCreate, onStart, onResume. Switches to the second tab – works onPause in OneActivityAnd then three methods TwoActivity: onCreate, onStart, onResume. And then when switching between tabs one goes to onPause, The other returns in onResume.

In the next lesson:

– Manually create the contents of the tab




Discuss in the forum [105 replies]

Leave a Comment