在 Android 上,如何以编程方式切换 Activity?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3591465/
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
On Android, how do you switch activities programmatically?
提问by dicroce
It seems like every example I can find of switching between activities involves creating an Intent and passing in the context of a View via an OnClickListener associated with a button.
似乎我能找到的在活动之间切换的每个示例都涉及创建一个 Intent 并通过与按钮关联的 OnClickListener 传入 View 的上下文。
But what if you just decide you need to switch activities? In my case, a preference value is causing an Activity switch.
但是,如果您只是决定需要切换活动怎么办?就我而言,偏好值导致活动切换。
How do you create an Intent that can cause an Activity switch without an associated OnClickListener?
你如何创建一个 Intent,它可以在没有关联的 OnClickListener 的情况下导致 Activity 切换?
回答by Chris Thompson
This should do it for you:
这应该为你做:
Intent myIntent = new Intent(this, MyActivityName.class);
startActivity(myIntent);
You can call that from anywhere in your current activity.
您可以从当前活动的任何地方调用它。
回答by diyoda_
It depends where you want to start the new activity in the code. You need the access to a Context reference to start a new activity( For example: onPostExecute in AsyncTask). Please have a look at this.
这取决于您希望在代码中从何处开始新活动。您需要访问 Context 引用才能启动新活动(例如:AsyncTask 中的 onPostExecute)。请看看这个。
Even though it is basically this.
虽然基本上就是这样。
Intent myIntent = new Intent(this, ActivityName.class);
startActivity(myIntent);
It can be something like this as well
它也可以是这样的
Intent myIntent = new Intent(context, ActivityName.class);
context.startActivity(myIntent);
回答by Jawad Zeb
I have the shortest Version
我有最短的版本
startActivity(new Intent(CurrentActivity.this,ActivityYouWantToOpen.class));
回答by Srinivas
when ever u want to switch activity . u can call these code .
当你想切换活动时。你可以调用这些代码。
Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);
You can write this code in PreferenceChangeListener.
您可以在 PreferenceChangeListener 中编写此代码。
回答by Konstantin Burov
Use PreferenceChangeListener :)
使用 PreferenceChangeListener :)
回答by Rakesh Gondaliya
You can create intent
in the main activity like this
您可以intent
像这样在主要活动中创建
Intent intent = new Intent(FirstActivity.this, second.class);
startActivity(intent);
If you are waiting for result from the second then you should use
如果您正在等待第二个结果,那么您应该使用
StartActivityforresult(intent,request code)
.
StartActivityforresult(intent,request code)
.
Request code can be any integer
.
请求代码可以是任何integer
.
回答by Yamikani Sita
startActivity (new Intent (Thisactivity.this, Nextactivity.class));
Don't forget to add activity to your manifest
不要忘记将活动添加到您的清单中
<Activity android:name=".NextActivity>
回答by Ganesh Garad
Firstly you need to create UI for a button by using layout intro_activity_1.XML file. After that set id for button group using android:id="@+id/button"
首先,您需要使用布局 intro_activity_1.XML 文件为按钮创建 UI。之后使用 android:id="@+id/button" 为按钮组设置 id
Example:
例子:
intro_activity_1.xml
intro_activity_1.xml
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:text="NEXT" />
Now change your java class of first activity. In this example, we change java file of IntroActivity1.java
现在更改您的第一个活动的 java 类。在这个例子中,我们改变了 IntroActivity1.java 的 java 文件
Example:
例子:
IntroActivity1.java
介绍活动1.java
//header, import and package data
public class IntroActivity1 extends AppCompatActivity {
Button next_btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intro_activity_1);
next_btn=(Button)findViewById(R.id.button);//button class
next_btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
//Start new activity class
Intent myIntent=new Intent(IntroActivity1.this,IntroActivity2.class);
startActivity(myIntent);
}
});
}
For More details about activity changer visit : https://answerdone.blogspot.com/2018/01/how-to-change-new-activity-in-android.html
有关活动更改器的更多详细信息,请访问:https: //answerdone.blogspot.com/2018/01/how-to-change-new-activity-in-android.html