界面activity_main.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="com.example.activity_05.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="22dp" android:text="启动第二个Activity" /> </RelativeLayout>
界面second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is the second activity" /> </LinearLayout>
MainActivity.java
package com.example.activity_05; 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.widget.Button; import android.view.View.OnClickListener; public class MainActivity extends ActionBarActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("MainActivity onCreate:第一次调用Activity的时候执行"); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); MainActivity.this.startActivity(intent); } }); } @Override protected void onResume() { System.out.println("MainActivity onResume"); super.onResume(); } //以上三个方法执行完毕之后这个Activity就可以使用了 @Override protected void onStart() { System.out.println("MainActivity onStart:Activity处于可见状态的时候调用"); super.onStart(); } @Override protected void onRestart() { System.out.println("MainActivity onRestart:Activity又可以得到用户焦点的时候调用"); System.out.println("当从第二个Activity又回到第一个Activity时调用此方法"); System.out.println("然后调用onResume方法就可以显示出来"); System.out.println("然后调用第二个Activity的onStop、onDestroy。第二个Activity就被销毁掉了"); super.onRestart(); } @Override protected void onPause() { System.out.println("MainActivity onPause"); System.out.println("如果在执行当前Activity的时候又启动了一个Activity就会调用当前Activity的此方法"); System.out.println("然后依次调用新启动的Activity的onCreate、onStart、onResume"); System.out.println("然后调用第一个Activity的onStop方法"); super.onPause(); } @Override protected void onStop() { System.out.println("MainActivity onStop"); //如果第二个Activity把第一个Activity完全遮挡住就调用这个函数 super.onStop(); } @Override protected void onDestroy() { System.out.println("MainActivity onDestroy"); super.onDestroy(); } }
SecondActivity.java
package com.example.activity_05; import android.app.Activity; import android.os.Bundle; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); } @Override protected void onStart() { System.out.println("SecondActivity onStart"); super.onStart(); } @Override protected void onRestart() { System.out.println("SecondActivity onRestart"); super.onRestart(); } @Override protected void onResume() { System.out.println("SecondActivity onResume"); super.onResume(); } @Override protected void onPause() { System.out.println("SecondActivity onPause"); super.onPause(); } @Override protected void onStop() { System.out.println("SecondActivity onStop"); super.onStop(); } @Override protected void onDestroy() { System.out.println("SecondActivity onDestroy"); super.onDestroy(); } }
AndroidManifest.xml
<activity android:name=".SecondActivity" android:label="第二个Activity"/>
日志输出如下
每次显示栈顶Activity的内容
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。