how to call the start activity from one java class

落花浮王杯 提交于 2020-01-23 06:43:05

问题


Is it possible to start an Activty using an Intent in a general java class which 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

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?


回答1:


To start an Activity you need a Context.

The method startActivity(Intent intent) is inherited from the Context class. As it can be seen in the Documentation

Also an explicit Intent itself needs a Context in its constructor.

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

void method() {
    startActivity(new Intent(this, ActivityName.class));
} 

If you do not want to extend Activity you can pass the Context as an argument.

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);
    }
}



回答2:


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.

STEP 1:

As EndZeit pointed out, you need context of your MainActivity class in your Custom.java class to start a new activity from that point.

Custom custom = new Custom(MainActivity.this);

STEP 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:

Now, start the new activity:

public void method() {
    Intent intent = new Intent(context, NewActivity.class);
    context.startActivity(intent);
}

You're done :)




回答3:


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" />



回答4:


Just use the following code from any Java class:

Intent i = new Intent(getContext(),TargetActivity.class);
getContext().startActivity(i);



回答5:


When a class extens Activity then turns to Activity class. So, your both classes, subactivity and testclass, activity class.

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

Android activity – from one screen to another screen




回答6:


In Kotlin this is how you can do it

I am asuming that you have context available

val intent = Intent(context, YourActivity::class.java)
context.startActivity(intent)
(context as Activity).finish()


来源:https://stackoverflow.com/questions/21888385/how-to-call-the-start-activity-from-one-java-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!