活动和子活动 [英] Activities and sub activities

查看:89
本文介绍了活动和子活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有5个不同的活动(它们命名为A,B,C,D,E),并有一个菜单(在屏幕的底部由按钮),以A,B ... E <之间切换/ P>

当我显示,有一些按钮,用户利用它们的推移另一个活动A1在A的顶部,然后从那里,他进入A12,所以我的活动堆栈是像A> A1> A12,现在在对B菜单按钮的用户点击,然后他继续B1从那里。 (堆栈会像A> A1> A12> B> B1)现在,如果用户preSS按钮切换回A,因此它应该显示A12,因为它是在A的顶部。

我怎么管理这个scanario?我没有暗示可言

解决方案

相反按钮在屏幕的底部,用TabHost和的ActivityGroup所选择的选项卡下显示多活动。

 &LT; TabHost
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@机器人:ID / tabhost
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT&GT;

  &LT;的LinearLayout
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;

    &LT;的FrameLayout
      机器人:ID =@机器人:ID / tabcontent
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_weight =1/&GT;

    &LT; TabWidget
      机器人:ID =@机器人:ID /标签
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =WRAP_CONTENT
      机器人:layout_weight =0/&GT;

  &LT; / LinearLayout中&GT;

&LT; / TabHost&GT;
 

主要活动:

  / **
 *活动显示的主选项卡和管理的独立活动
 *选中的标签。
 * /
公共类MainActivity扩展TabActivity {

  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);


    //加载主视图
    的setContentView(R.layout.main_activity);

    //获取TabHost
    TabHost tabHost = getTabHost();

    // resusable则tabspec为每个标签
    TabHost.TabSpec规范;

    //可重复使用的意向为每个标签
    意向意图;

    //第一个选项卡
    意图=新的意图(这一点,TabGroupActivity.class);
    规格= tabHost.newTabSpec(第一)
        .setIndicator(createTabIndicatorView(tabHost,第一,R.drawable.ic_tab_first))
        .setContent(意向);
    tabHost.addTab(规范);

    //第二个选项卡
    意图=新的意图(这一点,TabGroupActivity.class);
    规格= tabHost.newTabSpec(二)
        .setIndicator(createTabIndicatorView(tabHost,二,R.drawable.ic_tab_second))
        .setContent(意向);
    tabHost.addTab(规范);

    tabHost.setCurrentTab(0);
  }

  / **
   *创建的标签与自定义布局。
   *
   *参数tabHost标签主机
   * @参数tabTitle的选项卡的标题
   * @参数图标的标签的图标
   返回:视图重新presenting单卡
   * /
  私人查看createTabIndicatorView(TabHost tabHost,CharSequence的tabTitle,INT图标){
    LayoutInflater充气=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看tabIndicator = inflater.inflate(R.layout.tab_indicator,tabHost,假);
    最后的TextView电视=(TextView中)tabIndicator.findViewById(R.id.title);
    tv.setText(tabTitle);
    最后ImageView的ICONVIEW =(ImageView的)tabIndicator.findViewById(R.id.icon);
    iconView.setImageDrawable(getResources()getDrawable(图标)。);
    返回tabIndicator;
  }

}
 

该activty组:

  / **
 *此活动的目的是管理的一个选项卡中的活动。注意:
 *儿童活动能够处理关键presses他们在这里看到过。
 * /
公共类TabGroupActivity扩展的ActivityGroup {

  私人的ArrayList&LT;字符串&GT; mIdList;

  公共TabGroupActivity(){
  }

  @覆盖
  公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    如果(mIdList == NULL){
      mIdList =新的ArrayList&LT;字符串&GT;();
    }

    startChildActivity(firstChildActivity,新的意向(这一点,FirstChildActivity.class));
  }

  / **
   *当这一个子活动调用它的完成方法这就是所谓的。
   *在此实现调用{@link LocalActivityManager#destroyActivity}
   *孩子的活动,并启动previous活动。如果最后孩子
   *活动只叫光洁度(),这个活动(父),调用完成对
   *完成整个组。
   * /
  @覆盖
  公共无效finishFromChild(活动子){
    LocalActivityManager经理= getLocalActivityManager();
    INT指数= mIdList.size() -  1;

    如果(指数&LT; 1){
      完();
      返回;
    }

    manager.destroyActivity(mIdList.get(指数),TRUE);
    mIdList.remove(指数);
    指数 -​​ ;
    字符串lastId = mIdList.get(指数);
    意图lastIntent = manager.getActivity(lastId).getIntent();
    窗口newWindow = manager.startActivity(lastId,lastIntent);
    的setContentView(newWindow.getDecorView());
  }

  / **
   *启动的活动作为一个孩子的活动了这一点。
   *
   *要启动的活动的@参数标识的唯一标识符。
   * @参数意图要启动描述酶活性的意图。
   * @throws android.content.ActivityNotFoundException。
   * /
  公共无效startChildActivity(字符串ID,意图意图){
    窗口窗口= getLocalActivityManager()。startActivity(身份证,
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    如果(窗口!= NULL){
      mIdList.add(同上);
      的setContentView(window.getDecorView());
    }
  }

  / **
   *的主要目的是,以prevent系统之前
   * android.os.Build.VERSION_ codeS.ECLAIR与调用它们的默认
   * KeyEvent.KEY code_BACK的onkeydown中。
   * /
  @覆盖
  公共布尔的onkeydown(INT键code,KeyEvent的事件){
    如果(键code == KeyEvent.KEY code_BACK){
      // preventing默认实现previous到
      // android.os.Build.VERSION_ codeS.ECLAIR
      返回true;
    }
    返回super.onKeyDown(键code,事件);
  }

  / **
   *覆盖的KeyEvent.KEY code_BACK,使所有的默认实现
   *系统调用onBack pressed()。
   * /
  @覆盖
  公共布尔的onkeyup(INT键code,KeyEvent的事件){
    如果(键code == KeyEvent.KEY code_BACK){
      onBack pressed();
      返回true;
    }
    返回super.onKeyUp(键code,事件);
  }

  / **
   *如果孩子活动处理KeyEvent.KEY code_BACK。简单地覆盖,并添加
   *此方法。
   * /
  @覆盖
  公共无效onBack pressed(){
    INT长度= mIdList.size();
    如果(长度大于1){

      活动电流= getLocalActivityManager()getActivity(mIdList.get(长度 -  1))。
      current.finish();

    } 其他 {
      super.onBack pressed();
    }
  }

}
 

My Application has 5 different activities ( name them as A,B,C,D,E ) and there is a menu (made by buttons on bottom of the screen), to switch between A,B...E

when I am displaying A, there are some button by which the user goes on another activity A1 on top of A, then from there he goes into A12, so my activity stack is something like A>A1>A12, now the user click on menu button for B, then he goes on B1 from there . (Stack would be like A>A1>A12>B>B1) now if user press the button to switch back to A, so it should display A12, as it is on top of A.

how do I manage this scanario? I have no hint at all

解决方案

Instead of buttons at bottom of the screen, use TabHost and ActivityGroup to display multiple activities under the selected tab.

<TabHost
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
      android:id="@android:id/tabcontent"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" />

    <TabWidget
      android:id="@android:id/tabs"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="0" />

  </LinearLayout>

</TabHost>

Main activity:

/**
 * Activity that displays the main tabs and manages separate activity for the
 * selected tab.
 */
public class MainActivity extends TabActivity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    // load main view
    setContentView(R.layout.main_activity);

    // get the TabHost
    TabHost tabHost = getTabHost();

    // resusable TabSpec for each tab
    TabHost.TabSpec spec;

    // reusable Intent for each tab
    Intent intent;

    // first tab
    intent = new Intent(this, TabGroupActivity.class);
    spec = tabHost.newTabSpec("first")
        .setIndicator(createTabIndicatorView(tabHost, "First", R.drawable.ic_tab_first))
        .setContent(intent);
    tabHost.addTab(spec);

    // second tab
    intent = new Intent(this, TabGroupActivity.class);
    spec = tabHost.newTabSpec("second")
        .setIndicator(createTabIndicatorView(tabHost, "Second", R.drawable.ic_tab_second))
        .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
  }

  /**
   * Creates tabs with custom layout.
   * 
   * @param tabHost the tab host
   * @param tabTitle the title of the tab
   * @param icon the icon of the tab
   * @return the view representing single tab
   */
  private View createTabIndicatorView(TabHost tabHost, CharSequence tabTitle, int icon) {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View tabIndicator = inflater.inflate(R.layout.tab_indicator, tabHost, false);
    final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
    tv.setText(tabTitle);
    final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon);
    iconView.setImageDrawable(getResources().getDrawable(icon));
    return tabIndicator;
  }

}

The activty group:

/**
 * The purpose of this Activity is to manage the activities in a tab. Note:
 * Child Activities can handle Key Presses before they are seen here.
 */
public class TabGroupActivity extends ActivityGroup {

  private ArrayList<String> mIdList;

  public TabGroupActivity() {
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (mIdList == null) {
      mIdList = new ArrayList<String>();
    }

    startChildActivity("firstChildActivity", new Intent(this, FirstChildActivity.class));
  }

  /**
   * This is called when a child activity of this one calls its finish method.
   * This implementation calls {@link LocalActivityManager#destroyActivity} on
   * the child activity and starts the previous activity. If the last child
   * activity just called finish(),this activity (the parent), calls finish to
   * finish the entire group.
   */
  @Override
  public void finishFromChild(Activity child) {
    LocalActivityManager manager = getLocalActivityManager();
    int index = mIdList.size() - 1;

    if (index < 1) {
      finish();
      return;
    }

    manager.destroyActivity(mIdList.get(index), true);
    mIdList.remove(index);
    index--;
    String lastId = mIdList.get(index);
    Intent lastIntent = manager.getActivity(lastId).getIntent();
    Window newWindow = manager.startActivity(lastId, lastIntent);
    setContentView(newWindow.getDecorView());
  }

  /**
   * Starts an Activity as a child Activity to this.
   * 
   * @param Id Unique identifier of the activity to be started.
   * @param intent The Intent describing the activity to be started.
   * @throws android.content.ActivityNotFoundException.
   */
  public void startChildActivity(String Id, Intent intent) {
    Window window = getLocalActivityManager().startActivity(Id,
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
      mIdList.add(Id);
      setContentView(window.getDecorView());
    }
  }

  /**
   * The primary purpose is to prevent systems before
   * android.os.Build.VERSION_CODES.ECLAIR from calling their default
   * KeyEvent.KEYCODE_BACK during onKeyDown.
   */
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      // preventing default implementation previous to
      // android.os.Build.VERSION_CODES.ECLAIR
      return true;
    }
    return super.onKeyDown(keyCode, event);
  }

  /**
   * Overrides the default implementation for KeyEvent.KEYCODE_BACK so that all
   * systems call onBackPressed().
   */
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      onBackPressed();
      return true;
    }
    return super.onKeyUp(keyCode, event);
  }

  /**
   * If a Child Activity handles KeyEvent.KEYCODE_BACK. Simply override and add
   * this method.
   */
  @Override
  public void onBackPressed() {
    int length = mIdList.size();
    if (length > 1) {

      Activity current = getLocalActivityManager().getActivity(mIdList.get(length - 1));
      current.finish();

    } else {
      super.onBackPressed();
    }
  }

}

这篇关于活动和子活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆