如何从一个 Java 类调用开始活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21888385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call the start activity from one java class
提问by Saravanan
Is it possible to start an Activty
using an Intent
in a general java
class which extends Activity
?
是否可以在一般类中开始Activty
using an ?Intent
java
extends Activity
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.app.Activity;
import android.content.Intent;
public class SubActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
And a general java class like the following
和一个像下面这样的通用java类
class TestClass extends Activity{
void firstsem(){
Intent t = new Intent(this, SubActivity.class);
t.putExtra("sub1","chemistry");
startActivity(t);
}
}
Is it possible to start an Activity from an general java class? Could anybody show me how to achieve this?
是否可以从通用 Java 类启动 Activity?有人可以告诉我如何实现这一目标吗?
回答by Hamid Shatu
When a class extens Activity
then turns to Activity class. So, your both classes, subactivity
and testclass
, activity class.
当一个类扩展Activity
然后转向 Activity 类。所以,你的两个类,subactivity
和testclass
,活动类。
Yeah, you can start a activity from another class.
是的,您可以从其他班级开始一项活动。
Follow the below tutorial link...you will get to know how to start an activity from another activity
按照以下教程链接...您将了解如何从另一个活动开始活动
回答by Phant?maxx
Change this line:
改变这一行:
Intent t= new Intent(testclass.this,Subject.class);
to:
到:
Intent t= new Intent(testclass.this,subactivity.class);
Also, put a reference to subactivity in your Manifest file
此外,在您的清单文件中添加对子活动的引用
Something like:
就像是:
<activity android:name="com.example.app.subactivity" />
回答by Endzeit
To start an Activity
you need a Context
.
要启动一个Activity
你需要一个Context
.
The method startActivity(Intent intent)
is inherited from the Context
class. As it can be seen in the Documentation
该方法startActivity(Intent intent)
是从Context
类继承的。正如它在文档中看到的那样
Also an explicit Intent
itself needs a Context
in its constructor.
显式Intent
本身也需要Context
在其构造函数中使用 a 。
Intent(Context packageContext, Class<?> cls)
As Activity extends Context
and you've extended Activity you can use your own class as Context.
And thus simply call
由于Activity extends Context
您已经扩展了 Activity,您可以使用自己的类作为 Context。因此只需调用
void method() {
startActivity(new Intent(this, ActivityName.class));
}
If you do not want to extend Activity you can pass the Context as an argument.
如果您不想扩展 Activity,您可以将 Context 作为参数传递。
public static void startActivity(Context context) {
context.startActivity(new Intent(context, ActivityName.class));
}
BASIC IMPLEMENTATION
基本实现
public class ActivityA extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate();
}
public void onClick(View view) {
ActivityStarter.startActivityB(this);
}
}
public class ActivityB extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate();
}
}
public class ActivityStarter {
public static void startActivityB(Context context) {
Intent intent = new Intent(context, ActivityB.class);
intent.putExtra("sub1","chemistry");
context.startActivity(intent);
}
}
回答by Akeshwar Jha
Yeah, it is possible. And extending the activity is not necessary too. Let's name our custom class as Custom.java
Let's assume that we need to start the NewActivity
class when method()
is called in our Custom class.
是的,这是可能的。也没有必要扩展活动。让我们将自定义类命名为Custom.java
假设我们需要在自定义类中调用NewActivity
时启动method()
该类。
STEP 1:
第1步:
As EndZeitpointed out, you need context of your MainActivity class in your Custom.java class to start a new activity from that point.
正如EndZeit指出的那样,您需要 Custom.java 类中 MainActivity 类的上下文,以便从该点开始新活动。
Custom custom = new Custom(MainActivity.this);
Custom custom = new Custom(MainActivity.this);
STEP 2:
第2步:
In the custom class, make a constructor and receive the context passed:
在自定义类中,创建一个构造函数并接收传递的上下文:
Private Context context;
public Custom (Context context) {
this.context = context;
}
STEP 3:
第 3 步:
Now, start the new activity:
现在,开始新的活动:
public void method() {
Intent intent = new Intent(context, NewActivity.class);
context.startActivity(intent);
}
You're done :)
你完成了 :)
回答by Zohab Ali
In Kotlin this is how you can do it
在 Kotlin 中,这就是你可以做到的
I am asuming that you have context
available
我asuming你有context
可用
val intent = Intent(context, YourActivity::class.java)
context.startActivity(intent)
(context as Activity).finish()
回答by cycypher
Just use the following code from any Java class:
只需使用任何 Java 类中的以下代码:
Intent i = new Intent(getContext(),TargetActivity.class);
getContext().startActivity(i);