English 中文(简体)
完全停止或重新启动安更应用程序中的所有活动,以反映新的语言选择
原标题:Fully stopping or restarting all activities in an Android app to reflect new language choice
  • 时间:2012-05-23 21:22:52
  •  标签:
  • android

我正研究一个需要双语(英语/西班牙语)的Android应用软件。我允许用户从首选语言中选择所需的语言,并根据所选语言对应用程序的主要活动(TabActicity子类)进行以下局部修改:

private void setApplicationLanguage(String languageCode)
{
    // Set the locale to the specified language code.
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = new Locale(languageCode.toLowerCase());
    res.updateConfiguration(conf, dm);
}

主活动包含四个标签, 每个标签都有自己的活动( 有时活动组 ) 。 我希望发生的情况是, 当语言变化时, < em> all 开放活动只是重新开始, 导致字符串以合适的语言重新装入。 也可以接受( 但不太可取) : 全部销毁所有开放活动, 要求用户再次手动启动应用程序, 因此所有字符串都将用合适的语言重新装入 。

我尝试从导致该活动停止的主要活动中调用finish () 。 但是,当我重新启动应用程序时,由主要活动“主持”的“儿童活动”(每个标签各一张)仍以先前语言出现。 我猜这是因为儿童活动对我而言不是反复的“完成 ” 。

我还尝试了包含/ 排除 Anderroid Manifest. xml 中关于主要活动和其他活动的“ 本地” 属性, 但我从未看到活动在本地变化时重新开始。 但是, “ 重新启动” 本身会如何表现呢? 它会称之为 < code> onCreate () 或简单的 < code> onResume () 或其它东西吗? 如果这个方法会自动重启活动, 在本地变化时呼叫 < code > onCreate () < / code >, 那么它会听起来像是关于如何实现这一点的具体指导, 会是我案子中的最佳途径 。

解决方案需要运行在Android API 7 级上,因此无法创建新的 < code> Intent ,其旗帜为 < code> FLAG_ACTIVITY_CLEAR_TASK ,显然无法使用。

最佳回答

您可以使用 BroadcastReceiver 作为用于标签内容的每个 Actionience 的内层。

onResume 中注册接收器,在 on Pause 中不注册。

activity 发送粘贴广播时,当位置被更改时, 并使用 registerRecepier 的结果在 onResume 中检索 Intent , 以查看本地是否已经更改。 这对于任何可见的 Acticity 也应有效, 使其能够动态更新数据 。

问题回答

我实施了一个 LocalBroadcastManager , 告诉有兴趣的当事人当地环境的变化。 问题是我无法更新我想要更新的一切, 比如标签上的标签。 最有效的是“ main” (Tab) 活动中的以下内容:

    Intent intent = new Intent(mainActivity.getApplicationContext(), MainTabActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    mainActivity.startActivity(intent);




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签