简体   繁体   English

Android - 如何从实例启动新活动

[英]Android - How To Start New Activity From an Instance

i'm new in Android developing. 我是Android开发的新手。
I've to start a new Activity. 我要开始一个新的活动。 Commonly, i would write the following code: 通常,我会写下面的代码:

Intent i = new Intent(Activity1.this, Activity2.class);
Activity1.this.startActivity(i);

but now i need to start a new activity from an instance of that activity (because i don't want to start a generic activity of that type, i need to call his constructor to define his attributes). 但现在我需要从该活动的实例开始一个新活动(因为我不想启动该类型的泛型活动,我需要调用他的构造函数来定义他的属性)。 Something like this: 像这样的东西:

Activity2 instance = new Activity2(parameters);
Intent i = new Intent(Activity1.this, instance);
Activity1.this.startActivity(i);

Is it possible? 可能吗?

I think you're better off to add a bundle to your intent, and read the info out that. 我认为你最好添加一个捆绑到你的意图,并阅读信息。 You pass your parameters with that bundle. 您使用该包传递参数。

example: 例:

    Intent myIntent = new Intent(this, BlipImageSender.class);
    Bundle paramets = new Bundle();

 paramets.putString("YOUR_PARAM_IDENT","your_parameter_value");

 myIntent.putExtras(paramets);
 this.startActivity(myIntent);

and in your class: 在你的班上:

String your_param_value = getIntent().getExtras().getString("YOUR_PARAM_IDENT");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM