首页 > 移动开发 > Android > 正文
1.5.4 TabActivity类
2015-04-21 15:56:59     我来说两句      
收藏    我要投稿
当loadTabFragments()方法没有找到选项卡时,就会启动代码清单1-7中的活动。值得注意的是,与本例中的其他活动类相比它所不做的事情。它的存在是为了加载包含在大屏幕上列表右边显示的片段的布局,但它没有对屏幕尺寸和布局,以及布局所使用的Fragment子类做任何决定或假设。其结果是,在小屏幕上将启动这个活动,并显示所需的选项卡,而在较大的屏幕上将显示在屏幕右侧。
代码清单1-7:TabActivity.java
package com.enterpriseandroidbook.fragmentframework;
      
import android.app.ActionBar;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
      
public class TabActivity extends TabbedActivity {

// String for logging the class name
private final String CLASSNAME = getClass().getSimpleName();

// Turn logging on or off
private static final boolean L = true;
      
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
      
// To keep this method simple
doCreate(savedState);

// If we had state to restore, we note that in the log message
if (L) Log.i(CLASSNAME, "onCreate" +
(null == savedState ? " Restored state" : ""));
}
      
@Override
protected void onRestart() {
super.onRestart();
// Notification that the activity will be started
if (L) Log.i(CLASSNAME, "onRestart");
}
      
@Override
protected void onStart() {
super.onStart();
// Notification that the activity is starting
if (L) Log.i(CLASSNAME, "onStart");
}
      
@Override
protected void onResume() {
super.onResume();
// Notification that the activity will interact with the user
if (L) Log.i(CLASSNAME, "onResume");
}
      
protected void onPause() {
super.onPause();
// Notification that the activity will stop interacting with the user
if (L) Log.i(CLASSNAME, "onPause" + (isFinishing() ? " Finishing" : ""));
}
      
@Override
protected void onStop() {
super.onStop();
// Notification that the activity is no longer visible
if (L) Log.i(CLASSNAME, "onStop");
}

@Override
protected void onDestroy() {
super.onDestroy();
// Notification the activity will be destroyed
if (L) Log.i(CLASSNAME, "onDestroy"
// Are we finishing?
+ (isFinishing() ? " Finishing" : ""));
}
      
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState(outState);
      
// Called when state should be saved
if (L) Log.i(CLASSNAME, "onSaveInstanceState");
      
}
      
@Override
protected void onRestoreInstanceState(Bundle savedState) {
super.onRestoreInstanceState(savedState);
if (null != savedState) restoreState(savedState);

// If we had state to restore, we note that in the log message
if (L) Log.i(CLASSNAME, "onRestoreInstanceState" +
(null == savedState ? " Restored state" : ""));
}
      
///////////////////////////////////////////////////////////////////////
// The minor lifecycle methods - you probably won't need these
///////////////////////////////////////////////////////////////////////
      
@Override
protected void onPostCreate(Bundle savedState) {
super.onPostCreate(savedState);
if (null != savedState) restoreState(savedState);

// If we had state to restore, we note that in the log message
if (L) Log.i(CLASSNAME, "onCreate" + (null == savedState ? 
      " Restored state" : ""));
      
}
      
@Override
protected void onPostResume() {
super.onPostResume();
// Notification that resuming the activity is complete
if (L) Log.i(CLASSNAME, "onPostResume");
}
      
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
// Notification that user navigated away from this activity
if (L) Log.i(CLASSNAME, "onUserLeaveHint");
}

///////////////////////////////////////////////////////////////////////
// Overrides of the implementations ComponentCallbacks methods in Activity
///////////////////////////////////////////////////////////////////////

@Override
public void onConfigurationChanged(Configuration newConfiguration) {
super.onConfigurationChanged(newConfiguration);

// This won't happen unless we declare changes we handle in the manifest
if (L) Log.i(CLASSNAME, "onConfigurationChanged");
}

@Override
public void onLowMemory() {
// No guarantee this is called before or after other callbacks
if (L) Log.i(CLASSNAME, "onLowMemory");
}
   
///////////////////////////////////////////////////////////////////////
// App-specific code here
///////////////////////////////////////////////////////////////////////
      
/**
* This is where we restore state we previously saved.
* @param savedState the Bundle we got from the callback
*/
private void restoreState(Bundle savedState) {
// Add your code to restore state here

}

/**
* Add this activity's state to the bundle and/or commit pending data
*/
private void saveState(Bundle state) {
// Add your code to add state to the bundle here
}

/**
* Perform initializations on creation of this Activity instance
* @param savedState
*/
private void doCreate(Bundle savedState) {
setContentView(R.layout.content_control_activity);
       
if (null != savedState) restoreState(savedState);

       ActionBar bar = getActionBar();
        bar.setDisplayShowTitleEnabled(false);
       bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Initialize the tabs
int names[] = {R.string.item, R.string.detail };
int fragments[] = { R.id.content_frag, R.id.detail_frag };
initializeTabs(0, names, fragments);

// Load data if there is some
loadTabFragments(getIntent().getExtras());
}
      
}

注意:请注意我们有时使用活动、片段、服务和视图等词来引用程序中精确命名的对应类。也就是说,我们使用这些名词代替对象名称,以使本书更便于阅读。你将在本书全书中见到这种模式的使用。

点击复制链接 与好友分享!回本站首页
分享到: 更多
您对本文章有什么意见或着疑问吗?请到论坛讨论您的关注和建议是我们前行的参考和动力  
上一篇:1.5.3 针对较小屏幕的main.xml和Subsidiary活动
下一篇:1.6 Android任务和进程模型
相关文章
图文推荐
排行
热门
文章
下载
读书

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做最好的IT技术学习网站