在Android中的选项卡中启动活动

这是交易。我有一个带有三个标签的应用程序。通过与选项卡中的项目的各种交互,我最终启动了其他活动。客户端已对此进行了审核,并希望在选项卡内“启动”活动,因此选项卡仍然可见,如果用户单击选项卡,则会返回到setContent函数中定义的原始活动。这是可能的,我将如何从其他活动中解决这个问题?(即子活动,而不是定义TabHost并有权调用setContent的那个)?



宝慕林4294392
浏览 591回答 3
3回答

阿波罗的战车

可以在选项卡中启动活动。因此,将tabspec内容设置为ActivityGroup而不是常规Activity。tabHost.addTab(tabHost.newTabSpec("Tab")                .setIndicator("Tab")                .setContent(new Intent(this, YourActivityGROUP.class)                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));然后,您可以从该ActivityGroup中启动另一个此类活动,该活动仅更新您所在选项卡的内容视图。class YourActivityGROUP extends ActivityGroup{@Overrideprotected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      //you van get the local activitymanager to start the new activity      View view = getLocalActivityManager()                                .startActivity("ReferenceName", new      Intent(this,YourActivity.class)                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))                                .getDecorView();       this.setContentView(view);   }}

Smart猫小萌

这是我的解决方案public class ActivityStack extends ActivityGroup {&nbsp; private Stack<String> stack;&nbsp; @Override&nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; if (stack == null) stack = new Stack<String>();&nbsp; &nbsp; //start default activity&nbsp; &nbsp; push("FirstStackActivity", new Intent(this, FirstStackActivity.class));&nbsp; }&nbsp; @Override&nbsp; public void finishFromChild(Activity child) {&nbsp; &nbsp; pop();&nbsp; }&nbsp; @Override&nbsp; public void onBackPressed() {&nbsp; &nbsp; pop();&nbsp; }&nbsp; public void push(String id, Intent intent) {&nbsp; &nbsp; Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));&nbsp; &nbsp; if (window != null) {&nbsp; &nbsp; &nbsp; stack.push(id);&nbsp; &nbsp; &nbsp; setContentView(window.getDecorView());&nbsp; &nbsp; }&nbsp; }&nbsp; public void pop() {&nbsp; &nbsp; if (stack.size() == 1) finish();&nbsp; &nbsp; LocalActivityManager manager = getLocalActivityManager();&nbsp; &nbsp; manager.destroyActivity(stack.pop(), true);&nbsp; &nbsp; if (stack.size() > 0) {&nbsp; &nbsp; &nbsp; Intent lastIntent = manager.getActivity(stack.peek()).getIntent();&nbsp; &nbsp; &nbsp; Window newWindow = manager.startActivity(stack.peek(), lastIntent);&nbsp; &nbsp; &nbsp; setContentView(newWindow.getDecorView());&nbsp; &nbsp; }&nbsp; }}启动标签Intent intent = new Intent().setClass(this, ActivityStack.class);TabHost.TabSpec spec = tabHost.newTabSpec("tabId")spec.setContent(intent);拨打下一个活动public class FirstStackActivity extends Activity {&nbsp; @Override&nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; TextView textView = new TextView(this);&nbsp; &nbsp; textView.setText("First Stack Activity ");&nbsp; &nbsp; textView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.setClass(getParent(), SecondStackActivity .class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActivityStack activityStack = (ActivityStack) getParent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activityStack.push("SecondStackActivity", intent);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; setContentView(textView);&nbsp; }}再次拨打电话public class SecondStackActivity extends Activity {&nbsp; @Override&nbsp; public void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; TextView textView = new TextView(this);&nbsp; &nbsp; textView.setText("First Stack Activity ");&nbsp; &nbsp; textView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.setClass(getParent(), ThirdStackActivity .class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActivityStack activityStack = (ActivityStack) getParent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; activityStack.push("ThirdStackActivity", intent);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; setContentView(textView);&nbsp; }}适用于模拟器2.2
打开App,查看更多内容
随时随地看视频慕课网APP