我是靠谱客的博主 开朗书包,最近开发中收集的这篇文章主要介绍TabHost实现多个Activity界面间切换,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1、主Activity继承至TabActivity

public class MainActivity extends TabActivity {
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		tabHost = getTabHost();

		addTab("act1", "界面1", MyActivity1.class);
		addTab("act2", "界面2", MyActivity2.class);
		addTab("act3", "界面3", MyActivity3.class);

		setContentView(tabHost);
	}

	/**
	 * 添加Activity标签
	 * @param tag	标识
	 * @param title	标签标题
	 * @param clazz 激活的界面
	 */
	private void addTab(String tag, String title, Class clazz) {
		TabSpec tabSpec = tabHost.newTabSpec(tag);
		tabSpec.setIndicator(title);
		
		Intent intent = new Intent(getApplicationContext(),clazz);
		tabSpec.setContent(intent);
		tabHost.addTab(tabSpec);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}
}



待切换的界面1:

public class MyActivity1 extends Activity {
	private static final String TAG = "MyActivity1";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG,"MyActivity1 创建了...");
		TextView tv = new TextView(this);
		tv.setText("我是界面1");
		tv.setGravity(Gravity.CENTER);
		tv.setTextSize(100);
		setContentView(tv);
	}
	
	@Override
	protected void onStop() {
		Log.i(TAG,"MyActivity1 onStop");
		super.onStop();
	}
	
	@Override
	protected void onPause() {
		Log.i(TAG,"MyActivity1 onPause");
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		Log.i(TAG,"MyActivity1 onResume");
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		Log.i(TAG,"MyActivity1 onDestroy");
		super.onDestroy();
	}
}

待切换的界面2:

public class MyActivity2 extends Activity {
	private static final String TAG = "MyActivity2";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG,"MyActivity2 创建了...");
		
		TextView tv = new TextView(this);
		tv.setText("我是界面2");
		tv.setGravity(Gravity.CENTER);
		tv.setTextSize(100);
		setContentView(tv);
	}
	
	@Override
	protected void onStop() {
		Log.i(TAG,"MyActivity2 onStop");
		super.onStop();
	}
	
	@Override
	protected void onPause() {
		Log.i(TAG,"MyActivity2 onPause");
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		Log.i(TAG,"MyActivity2 onResume");
		super.onResume();
	}
}

待切换的界面3:

public class MyActivity3 extends Activity {
	private static final String TAG = "MyActivity3";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG,"MyActivity3 创建了...");
		
		TextView tv = new TextView(this);
		tv.setText("我是界面3");
		tv.setGravity(Gravity.CENTER);
		tv.setTextSize(100);
		setContentView(tv);
	}
	
	@Override
	protected void onStop() {
		Log.i(TAG,"MyActivity3 onStop");
		super.onStop();
	}
	
	@Override
	protected void onPause() {
		Log.i(TAG,"MyActivity3 onPause");
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		Log.i(TAG,"MyActivity3 onResume");
		super.onResume();
	}
}


经测试,在切换的过程中,作为content的三个activity的生命周期方法,onStop()、onDestory()不会执行!

经查:getLocalActivityManager.dispath...可以分发各方法。从而激活对应activity的生命周期方法。(未测)

最后

以上就是开朗书包为你收集整理的TabHost实现多个Activity界面间切换的全部内容,希望文章能够帮你解决TabHost实现多个Activity界面间切换所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部