您好,登錄后才能下訂單哦!
界面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:第一次調(diào)用Activity的時候執(zhí)行"); 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(); } //以上三個方法執(zhí)行完畢之后這個Activity就可以使用了 @Override protected void onStart() { System.out.println("MainActivity onStart:Activity處于可見狀態(tài)的時候調(diào)用"); super.onStart(); } @Override protected void onRestart() { System.out.println("MainActivity onRestart:Activity又可以得到用戶焦點的時候調(diào)用"); System.out.println("當從第二個Activity又回到第一個Activity時調(diào)用此方法"); System.out.println("然后調(diào)用onResume方法就可以顯示出來"); System.out.println("然后調(diào)用第二個Activity的onStop、onDestroy。第二個Activity就被銷毀掉了"); super.onRestart(); } @Override protected void onPause() { System.out.println("MainActivity onPause"); System.out.println("如果在執(zhí)行當前Activity的時候又啟動了一個Activity就會調(diào)用當前Activity的此方法"); System.out.println("然后依次調(diào)用新啟動的Activity的onCreate、onStart、onResume"); System.out.println("然后調(diào)用第一個Activity的onStop方法"); super.onPause(); } @Override protected void onStop() { System.out.println("MainActivity onStop"); //如果第二個Activity把第一個Activity完全遮擋住就調(diào)用這個函數(shù) 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的內(nèi)容
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。