代码地址:http://download.csdn.net/detail/u013922681/8946779
下面我们通过代码依依讲解
package com.example.demo01;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("BaseActivity", getClass().getSimpleName()); //得当当前类的名称
}
public void actionStart(Context context,Class<?> className){
Intent intent = new Intent(context, className);
startActivity(intent);
}
}
package com.example.demo01;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.demo01.FirstActivtiy;
public class MainActivity extends BaseActivity {
private Button btnMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMain = (Button) findViewById(R.id.activity_main_btn_main);
btnMain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
actionStart(MainActivity.this,FirstActivtiy.class);
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/activity_main_btn_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到第FirstActivity" />
</RelativeLayout>
package com.example.demo01;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivtiy extends BaseActivity {
private Button btnFirst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
btnFirst = (Button) findViewById(R.id.activity_main_btn_first);
btnFirst.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SecondActivtiy.actionStart(FirstActivtiy.this,"下午将近四点","写博客有点晕,给好评~_~");
}
});
}
}
activity_first.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/activity_main_btn_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到第SecondActivity" />
</RelativeLayout>
package com.example.demo01;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivtiy extends BaseActivity {
private Button btnSecond;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv = (TextView) findViewById(R.id.activity_second_tv);
Intent intent = getIntent();
String data1 = intent.getStringExtra("data1");
String data2 = intent.getStringExtra("data2");
tv.setText("data1:"+data1+"\n"+
"data2:"+data2);
btnSecond = (Button) findViewById(R.id.activity_second_btn_second);
btnSecond.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
actionStart(SecondActivtiy.this, MainActivity.class);
}
});
}
/** * 启动activty * @param context * @param data1 附加参数1 * @param data2 附加参数2 */
public static void actionStart(Context context,String data1,String data2){
Intent intent = new Intent(context, SecondActivtiy.class);
intent.putExtra("data1", data1);
intent.putExtra("data2", data2);
context.startActivity(intent);
}
}
activty_second.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/activity_second_btn_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到第MainActivity" />
<TextView
android:layout_below="@id/activity_second_btn_second"
android:id="@+id/activity_second_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
最后注意配置活动,AndroidMinifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo01" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name="com.example.demo01.MainActivity" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.demo01.FirstActivtiy" android:label="@string/app_name_1"></activity>
<activity android:name="com.example.demo01.SecondActivtiy" android:label="@string/app_name_2"></activity>
</application>
</manifest>
效果截图:
**不知道大家是否注意到MainActivity跳转到FirstActivity和FirstActivity跳转到SecondActivity的actionStart()的来源不一样,前者来自于BaseActivity,而后者来自于SecondActivity的静态方法。之所以这样写,有如下两点:
1.在app开发中经常要写跳转的代码,所以是为了“偷懒”,简洁代码。
2.为什么不在BaseActivity里面重载SecondActivity里面的actionStart()方法,原因是公司的项目,有可能是联合开发,有一种情景就是SecondActivity是别人开发的,但是我要跳转到SecondActivity,而且需要传递数据,可问题是传递怎么的数据我们是不知道的,因此,为了不要让别人来询问你,这个SecondActivity需要传递什么的问题,那么在开发的时候就暴露一个方法给外界,方便跳转,提高开发效率>_<**