带有ListActivity BackButton的Android选项卡

时间:2013-04-24 18:37:37

标签: android android-layout android-intent android-widget

我有3个标签

TAB1,TAB2,TAB3

public Class TabLayout extends TabActivity {
    Tab1,Tab2,Tab3
}

每个标签包含一个ListActivity

这意味着

Tab1>>> Tab1ListActivity(带图片和文字的列表)

Tab2>>> Tab2ListActivity(带图片和文字的列表)

Tab3>>> Tab3ListActivity(带图片和文字的列表)

如果我点击任何列表项目(Tab1ListActivity,Tab2ListActivity等.......)

我转发到另一个活动(Tab1ListItemActivity,Tab2ListItemActivity等......)

但我的实际要求是假设我点击了Tab2ListItemActivity的后退按钮,那么我

应该可以转发到Tab2ListActivity

但是,如果我像这样写我的代码

public class Tab2ListItemActivity extends Activity
{

    //on Back Button Pressed

    Intent intent=new Intent(Tab2ListItemActivity.this,TabLayout.class);

    startActivity(intent);

    Tab2ListItemActivity.this.finish();
}

当我执行上面的代码时,我能够转发到我的TabLayout(Tab1ListActivity)但不转发到Tab2ListActivity ?, Beacuse

与Tab2ListActivity相关的Tab2ListItemActivity

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

  1. 您为什么要覆盖后退按钮?我的意思是,只要让默认行为发生,然后按后退按钮就会转到上一个活动。

  2. 如果你真的想按照自己的方式去做,那么你需要在启动TabLayout活动之前和 {{1}之内设置 tab id TabLayout 你需要检查 onCreate() 并启动相应的ListActivity。 tab id ,这就是为什么它总是带您到tab id is 1 by default

答案 1 :(得分:1)

为什么不保留默认行为(不要覆盖onBackPressed)?它将恢复到之前的活动。

解决方案1 ​​

不要完成TabLayout活动。并在Tab2ListItemActivity活动中保留后退按钮的默认实现。

解决方案2 (艰难,有力的方式):

在应该打开的标签上添加额外的意图。

Intent intent = new Intent(Tab2ListItemActivity.this, TabLayout.class);
intent.putExtra(TabLayout.EXTRA_TAB_TO_OPEN, 1);
startActivity(intent);
Tab2ListItemActivity.this.finish();
TabLayout活动中的

@Override
protected void onPostCreate(Bundle state) {
    super.onPostCreate(state);
    handleTab(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleTab(intent);
}


private void handleTab(Intent intent) {
    // init tabs
    final int tab = intent.getIntExtra(EXTRA_TAB_TO_OPEN, -1);
    if (tab != -1) {
        getTabHost().setCurrentTab(tab);
    }
}

答案 2 :(得分:0)

我遇到同样的问题,但我解决了这个问题。

<强> TabSample.class

public class TabSample extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TabHost tabHost = getTabHost();      

      tabHost.addTab(tabHost.newTabSpec("tab1")
              .setIndicator("OPT")
              .setContent(new Intent(this, TabGroup1Activity.class)));

      tabHost.addTab(tabHost.newTabSpec("tab2")
              .setIndicator("EDIT")
              .setContent(new Intent(this, TabGroup2Activity.class)));

      tabHost.setCurrentTab(1); 
    }
}

<强> TabGroup1Activity.class

public class TabGroup1Activity extends TabGroupActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        startChildActivity("OptionsActivity", new Intent(this,OptionsActivity.class));
    }
}

<强> OptionsActivity.class

public class OptionsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.optionspage);

        Button back = (Button) findViewById(R.id.BackButton1);
        Button next = (Button) findViewById(R.id.NextButton1);

        OnTouchListener backListener = new OnTouchListener() {
                public boolean  onTouch  (View  v, MotionEvent  event) {
                    if (event.getAction()==MotionEvent.ACTION_UP){
                        finish();
                    }
                    return false;
                }
            };

        OnTouchListener nextListener = new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                 if (event.getAction()==MotionEvent.ACTION_UP){
                     Intent edit = new Intent(getParent(), EditActivity.class);
                    TabGroupActivity parentActivity = (TabGroupActivity)getParent();
                    parentActivity.startChildActivity("EditActivity", edit);
                    return true;
                 }
                 return false;
            }
        };

        back.setOnTouchListener(backListener);
        next.setOnTouchListener(nextListener);
    }
}

最后通过 TabGroupActivity.class 保存所有的stat实例。

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

  @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());
  }

  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()); 
      }    
  }

  @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);
  }

  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          onBackPressed();
          return true;
      }
      return super.onKeyUp(keyCode, event);
  }

  public void  onBackPressed() {
      int length = mIdList.size();
      if ( length > 1) {
          Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
          current.finish();
      }  
  }
}

这是一个很棒的解决方案,您可以尝试这个。并进行适当的manifest条目。