最近有看Activity ,好多不懂,连个简单的跳转都不能顺利完成,结果当然,定实现个!哈
Activity 的跳转需要一个触发,不可能无缘无故的跳转,可以是最简单的 Button 触发,也可以其他的 OnClickListener(),各种跳转方法,这就需要大家去发掘,
找到了给 CC 一份啊!
跳转之一,直接设置 Intent 。
Intent intent = new Intent();
intent.setClass(this,xx.class); //其中第一个参数代表自身,在触发事件中,直接写 this 会出错,一般都是写类名.this。第二个参数就是跳转目标类。
startActivity(intent); // 开始跳转
跳转之二,给 Intent 设置 action 。
下面做的简单 demo 。
package com.gao;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class StartActivity extends Activity {
private Button mButton1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 = (Button) findViewById(R.id.button_show);
mButton1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("gao", " hello gao "); // 要传递的参数
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setAction("com.gao.another.BundleTestAc"); // 设置 action
startActivity(intent);
}
});
}
}
<activity android:name=".another.BundleTestAc"> // 这个是类自身
<intent-filter>
<action android:name="com.gao.another.BundleTestAc"></action> //如果用到 action 跳转,这个必须设置。
<category android:name="android.intent.category.DEFAULT" /> // 用到 intent-filter ,那这个属性也必须设置。
</intent-filter>
</activity>
就写这么多,我这个懒人不太喜欢深究,好多原理都是不懂,哎....