如何强制主Acivity等待子活动在Android的? [英] How to force main Acivity to wait for subactivity in Android?

查看:144
本文介绍了如何强制主Acivity等待子活动在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话的主要活动一个子活动。这个子活动应当采取一些数字来自用户(我使用编辑文本控件来实现这一点),将它们保存到静态变量的另一个类,并终止。我想主要活动等待子活动,但两者都只是同时运行。即使做某事一样,并不能帮助:

I am calling a subactivity from main activity. This subactivity should take few numbers from user (i'm using Edit text control to achieve this), save them to static variable in another class and terminate. I want main activity to wait for subactivity but both are just running simultaneously. Even doing sth like that doesn't help:

     Thread t = new Thread(new Runnable(){
     public void run(){
     Log.v("==================", "run "+new Date());
     startActivityForResult(new Intent(ctx,myCustomSubactivity.class),1);  
     } });
     Log.v("==================", "calling run "+new Date());
     t.start();      
     try {
        t.join();
    } catch (InterruptedException e) {Log.v("==================", "can't join");}
    Log.v("==================", "back from activity "+new Date());

你知道如何强制主要活动等待?不支持的Andr​​oid Thread.wait()方法(程序引发错误)。

do you know how to force main activity to wait? Thread.wait() method is not supported in Android(program throws error).

推荐答案

我同意尼古拉这绝对是Android的方式做到这一点。

I agree with Nikolay this is definitely the android way to do this.

先从子活动<一href="http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29"相对=nofollow> startActivityForResult 在次活动中使用<一个href="http://developer.android.com/reference/android/app/Activity.html#setResult%28int,%20android.content.Intent%29"相对=nofollow>的setResult 添加一个结果,code和意图所有你需要的数据包的数量。

Start the subactivity with startActivityForResult in the sub activity use setResult to add an result code and an intent with all the numbers you need in the data bundle.

在你的第一个活动覆盖<一href="http://developer.android.com/reference/android/app/Activity.html#onActivityResult%28int,%20int,%20android.content.Intent%29"相对=nofollow> onActivityResult 和检索意图的数字。

In your first activity overwrite onActivityResult and retrieve the numbers from the Intent.

如果您使用的静态变量,这似乎在第一时刻更容易,但它是非常不安全的,也有一些情况下,这可能无法正常工作。如果您的程序发送到后台的活动将被保存,但如果手机运行内存不足的系统将关闭您的程序和用户恢复后一切看起来就像用户离开的那一刻,但静态变量将被重新创建他们的初始值。

If you use the static variable this seems easier in the first moment but it is very insecure and there are some cases this may not work. If your program is send to the background your activities will be saved but if the phone runs low on memory the system will close your program and after the user resumes it everything looks like the moment the user left it but the static variables will be recreated to their initialization value.

试着习惯的方式是Android 活动周期工作。使用这种方法将导致更少的使用的内存和一个更好的用户体验。

Try to get used to the way the android activity lifecycle works. Using this approach will result in fewer used memory and a much better user experience.

这篇关于如何强制主Acivity等待子活动在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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