最基本的java文件格式:
package com.example.activity;
import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
activity跳转:intent方法。
package com.example.act;
import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.app.Activity;import android.content.Intent;public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println("onCreate"); Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener(){ public void onClick(View V){ Intent intent=new Intent(); intent.setClass(MainActivity.this, Two.class); startActivity(intent); } }); }@Override
protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("onStop"); Boolean vBoolean = isFinishing(); System.out.println(vBoolean); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("onRestart"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Boolean vBoolean = isFinishing(); System.out.println(vBoolean); System.out.println("onDestory"); }}
package com.example.act;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class Two extends ActionBarActivity {
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two); System.out.println("2onCreate"); Button button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener(){ public void onClick(View V){ Intent intent=new Intent(); intent.setClass(Two.this, MainActivity.class); startActivity(intent); } }); }
@Override
protected void onStart() { // TODO Auto-generated method stub super.onStart(); System.out.println("2onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); System.out.println("2onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); System.out.println("2onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); System.out.println("2onStop"); Boolean vBoolean = isFinishing(); System.out.println(vBoolean); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); System.out.println("2onRestart"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Boolean vBoolean = isFinishing(); System.out.println(vBoolean); System.out.println("2onDestory"); }}
还要在AndroidManifest.xml中添加
<activity
android:name=".Two" android:label="@string/title_activity_two" > </activity>
在位置:
<application
android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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=".Two" android:label="@string/title_activity_two" > </activity> </application>
两个activity直接传递消息:
intent来执行
核心代码:1
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_extra); Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(); intent.setClass(ExtraActivity.this, ResultActivity.class);//当前act和即将启动的act Bundle bundle = new Bundle(); bundle.putString("data", "1"); //见和值 intent.putExtras(bundle);//传递数据 startActivityForResult(intent,0); } }); }2
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); final Intent intent = getIntent(); String result = intent.getStringExtra("data");//获取键为data的字典所传递的字符串型数据 Toast.makeText(ResultActivity.this, result, Toast.LENGTH_SHORT).show();//显示出来 Button button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub setResult(RESULT_OK, intent); finish(); } }); }