如何使用的Java / Android的选项卡之间意图是什么? [英] How to use intent between tabs in java/Android?

查看:118
本文介绍了如何使用的Java / Android的选项卡之间意图是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何处理标签的意图。例如,我有两个选项卡的标签的活动。一是内容是一个文本视图。另一种是一个地图视图。当我点击的文本视图它重定向到TAB2。它可以通过setCurrentTab(1)或setCurrentTabByTag(TAB2)的方法很容易地实现。但我想lat和长值传递给地图活动砸脚。什么是使用意图或getter / setter方法​​在Java中更好的办法?你这是什么preFER?如果你的答案是意图。怎么样?

I would need to know how to handle the intent between tabs. For example, I have a tab activity with two tabs. First on content is a text view. another one is a map view. When i click that text view it redirects to the tab2. it can be easily achieved by setCurrentTab(1) or setCurrentTabByTag("tab2") methods. But i want to pass lat and long values to that Map Activity to drop pin. What is the better way to use intents or getter/setter in java? What do you prefer? if your answer is "Intents". How?

推荐答案

一个有趣的问题。我的理解是要切换到第二个选项卡上的第一个TabView的点击,而且特殊的数据传递给依赖于在第一个选项卡操作的第二个选项卡。

A interesting problem. I understand it that you want to change to the second tab on a click in the first tabview but also pass special data to the second tab that is dependent on the action in the first tab.

我一般会先从一个活动标签内的意见。然而,这是在标签主机配置的时刻完成。这意味着双方意图的一个用于让用户选择纬度长的活动,并显示经纬度长都在同一时间双头呆之一。

I would generally start your views inside the tabs with an activity. However this is done at the moment the tab host is configured. That means both intents the one for the activity that lets the user choose lat long and the one that shows lat long are openend at the same time.

因此​​,你不能将信息添加到使用并初始化标签主机的意图。
我不喜欢的解决方案,但在我脑海中的唯一的事情(使用不同的活动,为选项卡)使用自定义的应用程序存储到包含需要更新的第二个选项卡视图中的数据对象的引用。你必须有一个自己的类扩展应用程序并添加这个类中你表现,现在你可以调用getApplication在第一个选项卡将它转换为设置lat和长调用setCurrentTab之前您的应用程序类。在第二个选项卡中可以调用 getApplication( )再次,然后你会得到应用对象,它是在你的应用程序运行的每一刻,每一个活动一样。然后,再次将它转换到应用程序中的对象并获取经纬度和长期价值。看到这个页面就如何在谷歌裁判使用自定义的应用程序类。

Therefore you can't add the information to the intent used to intialize the tab host. I don't like the solution but the only thing that comes to my mind (using different activities for the tabs) is using a custom application that stores a reference to an object containing the data that you need to update the view in the second tab. You have to extend application with an own class and add this class in you manifest, now you can call getApplication in the first tab cast it to your application class set lat and long just before you call setCurrentTab. In the second tab you can call getApplication() again and you will then get the application object that is the same for every activity at every moment of your app running. You then have cast it again to your application object and retrieve lat and long value. See this page in the google refs on how to use a custom application class.

要使用自定义的应用程序类下面添加到您的应用程序的标签在你的清单:

To use a custom application class add the following to your application tag in your manifest:

  <application
  ...
   android:name=".somepackage.CustomAppClass"

这将告诉Android的实例化CustomAppClass作为你的应用程序类在您的应用程序启动的时刻。您需要扩展应用程序,以避免启动时的错误。

This will tell Android to instantiate the CustomAppClass as your Application class at the moment your app starts. You need to extend Application to avoid errors on start up.

另一种解决方案,但不是我想preFER是初始化自己的意见和享有,而不是活动初始化tabhost。随着一个选项卡中地图视图,这可能是非常沉重的记忆

Another solution but not the one I would prefer is to initialize the views yourself and initialize the tabhost with views and not activities. With a map view in one of the tabs this could be very memory heavy.

这篇关于如何使用的Java / Android的选项卡之间意图是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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