Android的活动类的构造函数的工作 [英] android activity class constructor working

查看:106
本文介绍了Android的活动类的构造函数的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在考虑采用Android活动的情况下,第一个工作方法是其的onCreate method..right?

When considering the case with android activity, the first method to work is its onCreate method..right?

假设我想通过2个参数到Android Activity类说 USERHOME 。对于正在创建活动类的构造函数 USERHOME 并接受PARAMS。

Suppose i want to pass 2 parameters to android activity class say UserHome . For that am creating the constructor of activity class UserHome and accepting the params.

但是,当我们调用一个活动,我们不进行初始化Activity类,我们只是创建 USERHOME 类的意图。

But when we are calling an activity we are not initializing the Activity class, we are just creating an intent of UserHome class.

那么,我们怎样才能通过PARAMS从另一个活动的该活动,而无需使用 intent.putExtra(是keyName,someValue中); 用法

Then how can we pass params to that activity from another activity without using intent.putExtra("keyName", "somevalue"); usage.

专家请说明我们如何能够掩盖这样的情况?

Experts please clarify how we can cover a situation like this.?

推荐答案

不知道为什么你不希望使用意图PARAMS。这就是他们来这里的目的。如果你需要传递您的应用程序来自不同的地方相同的参数,你可以考虑使用静态构造函数建立你的意图的要求为您。

Not sure why you would not want to use the intent params. That is what they are there for. If you need to pass the same parameters from different places in your application, you could consider using a static constructor that builds your intent request for you.

例如:

/**
 * Sample activity for passing parameters through a static constructor
 * @author Chase Colburn
 */
public class ParameterizedActivity extends Activity {

    private static final String INTENT_KEY_PARAM_A = "ParamA";

    private static final String INTENT_KEY_PARAM_B = "ParamB";

    /**
     * Static constructor for starting an activity with supplied parameters
     * @param context
     * @param paramA
     * @param paramB
     */
    public static void startActivity(Context context, String paramA, String paramB) {
        // Build extras with passed in parameters
        Bundle extras = new Bundle();
        extras.putString(INTENT_KEY_PARAM_A, paramA);
        extras.putString(INTENT_KEY_PARAM_B, paramB);

        // Create and start intent for this activity
        Intent intent = new Intent(context, ParameterizedActivity.class);
        intent.putExtras(extras);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Extract parameters
        Bundle extras = getIntent().getExtras();
        String paramA = extras.getString(INTENT_KEY_PARAM_A);
        String paramB = extras.getString(INTENT_KEY_PARAM_B);

        // Proceed as normal...
    }
}

您就可以通过调用启动您的活动:

You can then launch your activity by calling:

ParameterizedActivity.startActivity(这一点,第一个参数,第二个参数);

这篇关于Android的活动类的构造函数的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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