使用意图在 Android 选项卡之间切换

发布于 2024-11-03 14:49:24 字数 971 浏览 1 评论 0原文

我希望了解如何使用意图在选项卡之间切换。

在我的例子中,我使用两个选项卡:

Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;

// Capture tab
spec = tabHost.newTabSpec("capture").setIndicator(null,
  res.getDrawable(R.drawable.ic_tab_capture))
  .setContent(new Intent(this,CaptureActivity.class));
tabHost.addTab(spec);

// Upload tab
spec = tabHost.newTabSpec("upload").setIndicator(null,
    res.getDrawable(R.drawable.ic_tab_capture))
    .setContent(new Intent(this,ImageUpload.class));
tabHost.addTab(spec);

为了简化我的目标,我的 CaptureActivity.java 包含以下代码:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.capture);
  Intent intent = new Intent(this, ImageUpload.class);
  startActivityForResult(intent, 0);
}

我期望的是,应用程序应该立即切换到第二个选项卡(ImageUpload 活动),该选项卡工作正常,但是选项卡本身消失。我将 ImageUpload 活动作为独立页面获取,而不是在选项卡本身内。

知道那里出了什么问题吗?

I wish to find out how to switch between tabs using intents.

In my case I'm using the both tabs:

Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;

// Capture tab
spec = tabHost.newTabSpec("capture").setIndicator(null,
  res.getDrawable(R.drawable.ic_tab_capture))
  .setContent(new Intent(this,CaptureActivity.class));
tabHost.addTab(spec);

// Upload tab
spec = tabHost.newTabSpec("upload").setIndicator(null,
    res.getDrawable(R.drawable.ic_tab_capture))
    .setContent(new Intent(this,ImageUpload.class));
tabHost.addTab(spec);

To simplify my goal, my CaptureActivity.java includes the following code:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.capture);
  Intent intent = new Intent(this, ImageUpload.class);
  startActivityForResult(intent, 0);
}

What I'm expecting is, the app should switch instantly to the second tab (ImageUpload activity) which works fine, BUT the tabs themselves disappear. I get the ImageUpload activity as stand-alone page, not within the tab itself.

Any idea what's going wrong there ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缱绻入梦 2024-11-10 14:49:24

首先调用触发 ImageUpload.java 只会触发 ImageUpload.class,但 tabhost 肯定会消失。

您需要触发 MainActivity-TabActivity,其中添加了两个 tabHost

1.ImageUpload

2.CaptureActivity

这将维护您的 TabLayout

调用意图,如下所示


Intent i=new Intent(getApplicationContext(),MainActivity.class)//which is your mainActivity-Launcher
i.addFlags(Intent.FLAG_ACTIVITY_BRING_TO_FRONT);
startActivity(i);//will bring MainACtivity to Front

现在主要活动已经在运行指针直接指向 Main Activity 内的 onNewIntent()


重写 MainActivity 内的 onNewIntent()

==================================== ===

public class MainActivity extends TabActivty
{
 pubilc static TabHost tabhost;
 public void onCreate()
 {
  //where you added your two tab spec 
  //added them
 }
 public void onNewIntent(intent)
 {
   super.onNewIntent(intent);
   tabHost.setCurrentTab(1);//1-depends where you want to switch-tabIndexno, I assume Upload is at 2nd position (so "1")
  Activity currentActivity=getCurrentActivity();
           if(currentActivity instanceof Upload)
           {
               ((upload)currentActivity).onCreate();//watever method you want to call
           }
  }

}

First calling firing ImageUpload.java only fires ImageUpload.class but surely tabhost will disappear.

You need to fire your MainActivity-TabActivity where you added your two tabHost

1.ImageUpload

2.CaptureActivity

which will maintain your TabLayout

call intent like these


Intent i=new Intent(getApplicationContext(),MainActivity.class)//which is your mainActivity-Launcher
i.addFlags(Intent.FLAG_ACTIVITY_BRING_TO_FRONT);
startActivity(i);//will bring MainACtivity to Front

Now Main activity is already running so pointer directly goes to onNewIntent() inside Main Activity


Override onNewIntent() inside MainActivity

=====================================

public class MainActivity extends TabActivty
{
 pubilc static TabHost tabhost;
 public void onCreate()
 {
  //where you added your two tab spec 
  //added them
 }
 public void onNewIntent(intent)
 {
   super.onNewIntent(intent);
   tabHost.setCurrentTab(1);//1-depends where you want to switch-tabIndexno, I assume Upload is at 2nd position (so "1")
  Activity currentActivity=getCurrentActivity();
           if(currentActivity instanceof Upload)
           {
               ((upload)currentActivity).onCreate();//watever method you want to call
           }
  }

}
梦醒时光 2024-11-10 14:49:24

在创建 tabhost 的父活动类中,我实现了如下所示的方法:

public void switchTab(int tab){
        tabHost.setCurrentTab(tab);
}

在选项卡内部,我希望能够在内部切换到另一个选项卡,我创建了以下方法:

public void switchTabInActivity(int indexTabToSwitchTo){
        YOURTABHOSTACTIVITY ParentActivity;
        ParentActivity = (YOURTABHOSTACTIVITY) this.getParent();
        ParentActivity.switchTab(indexTabToSwitchTo);
}

如果您想要一个很好的示例这段代码,你可以看看我的MintTrack项目此处此处。

In the parent activity class where the tabhost is created I implemented a method like the one below:

public void switchTab(int tab){
        tabHost.setCurrentTab(tab);
}

Inside of the tab that I would like to be able to switch internally to another tab I created the method below:

public void switchTabInActivity(int indexTabToSwitchTo){
        YOURTABHOSTACTIVITY ParentActivity;
        ParentActivity = (YOURTABHOSTACTIVITY) this.getParent();
        ParentActivity.switchTab(indexTabToSwitchTo);
}

If you would like a good example of this code, you can take a look at my MintTrack project here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文