標籤:android style blog http io color ar os 使用
轉載請註明出處: http://blog.csdn.net/yangyu20121224/article/details/9059459
今天我們繼續來講解ActionBar的使用,不清楚這個類的讀者可以翻閱博主前幾篇的文章或者在網路上查閱相關
的資料,關於這個類講解的文章還是很多的,功能確實也很強大。好的,話不多說,讓我們趕快進入正題吧。
一、使用應用表徵圖實現層級導航
在預設的情況下,應用程式圖示顯示在操作欄的左邊。你能夠把這個表徵圖當做操作項來使用,應用程式可以在這
個表徵圖上響應以下兩個操作其中之一:
<1> 返回應用程式的“主”Activity;
<2> 嚮應用程式上級頁面導航。
要實現應用程式圖示能夠向上導航,首先就要在你的ActionBar中調用SetDisplayHomeAsUpEnabledtrue(true)方法。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); ...}
當使用者觸摸這個表徵圖時,系統會調用Activity帶有android.R.id.home ID的onOptionsItemSelected()方法。在這個響
應中,你既可以啟動主Activity,也可以返回你的應用程式結構化層次中使用者上一步操作的介面。
如果你要通過應用程式圖示的響應來返回主Activity,那麼就應該在Itent對象中包括
FLAG_ACTIVITY_CLEAR_TOP標識。用這個標記,如果你要啟動的Activity在當前任務中已經存在,那麼,堆棧中這
個Activity之上的所有的Activity都有被銷毀,並且把這個Activity顯示給使用者。添加這個標識往往是重要的,因為返回主
Activity相當與一個回退的動作,因此通常不應該再建立一個新的主Activity的執行個體,否則,最終可能會在當前任務中產
生一個很長的擁有多個主Activity的堆棧。
例如,下例的onOptionsItemSelected()方法實現了返回應用程式的主Activity的操作:
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent intent = new Intent(this, HomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); }}
在使用者從另一個應用程式進入當前Activity的情況下,你可能還想要添加FLAG_ACTIVITY_NEW_TASK標識。這
個標識確保在使用者返回首頁或上級頁面時,新的Activity不會被添加到當前的任務中,而是在屬於你自己的應用程式的
任務中啟動。例如,如果使用者通過被另一個應用程式調用的Intent對象啟動了你的應用程式中的一個Activity,那麼選
擇操作欄表徵圖來返回首頁或上級頁面時,FLAG_ACTIVITY_CLEAR_TOP標識會在屬於你的應用程式的任務中啟動這
個Activity(不是當前任務)。系統既可以用這個新的Activity做根Activity來啟動一個新的任務,也可以把存在背景擁
有這個Activity執行個體的一個既存任務帶到前台來,並且目標Activity會接受onNewIntent()回調。因此,如果你的Activity
要接收另一個應用程式的Intent對象,那麼通常應該給這個Intent對象添加FLAG_ACTIVITY_NEW_TASK標識,如:
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This is called when the Home (Up) button is pressed // in the Action Bar. Intent parentActivityIntent = new Intent(this, MyParentActivity.class); parentActivityIntent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(parentActivityIntent); finish(); return true; } return super.onOptionsItemSelected(item); }
噹噹前的activity從屬於一個不同應用的任務調出時,按標按鈕應該建立該應用的一個新任務。
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent upIntent = new Intent(this, MyParentActivity.class); if (NavUtils.shouldUpRecreateTask(this, upIntent)) { // This activity is not part of the application‘s task, so create a new task // with a synthesized back stack. TaskStackBuilder.from(this) .addNextIntent(new Intent(this, MyGreatGrandParentActivity.class)) .addNextIntent(new Intent(this, MyGrandParentActivity.class)) .addNextIntent(upIntent) .startActivities(); finish(); } else { // This activity is part of the application‘s task, so simply // navigate up to the hierarchical parent activity. NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); }
二、在Fragments中實現層級導航
當在應用中使用fragments時,單一的FragmentTransaction對象能夠表示環境的變化,應該被添加到back堆棧。
例如,如果你要實現一個master/detail流程,通過換出fragments,你應該確保在detail介面上點擊Back按鈕回退到
master介面。
getFragmentManager().beginTransaction().add(detailFragment, "detail") // Add this transaction to the back stack and commit. .addToBackStack() .commit();
如果FragmentTransaction對象在back堆棧上,activity的FragmentManager會處理Back按鈕的點擊事件。當這一
事件發生時,FragmentManager從back堆棧中彈出最近一次事務,並進行反向的行為。如果你的應用程式更新其他用
戶介面元素來反應當前fragment的狀態,例如action bar,記得在commit事務的時候更新UI。
getFragmentManager().addOnBackStackChangedListener( new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { // Update your UI here. } });
三、導航到外部介面
當啟動另一個應用程式的activity來允許使用者,寫一個郵件,或挑選一個照片附件,你一般不會想讓使用者在Launcher中重新啟動程式時回到這個介面。這會對使用者造成混淆。為了阻止這種事情的發生,簡單的對intent添加FLAG_ACTIVITY_CLEAR_WITH_TASK_RESET標籤來啟動外部的activity:
Intent externalActivityIntent = new Intent(Intent.ACTION_PICK); externalActivityIntent.setType("image/*"); externalActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(externalActivityIntent);
四、實現的
五、項目結構圖
六、詳細代碼編寫
1、這個項目的代碼不多,首先是主布局頁面,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"> <Button android:id="@+id/other_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="160dp" android:text="Other_Activity" /> <Button android:id="@+id/external_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/other_btn" android:text="External_Activity" /></RelativeLayout>
2、然後是另一個介面的布局,activity_other.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"></RelativeLayout>
3、主介面Activity類,MainActivity.java:
package com.yangyu.myactionbar04;import android.app.ActionBar;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } /** * 初始化組件 */ private void initView(){ final ActionBar actionBar = getActionBar(); actionBar.setHomeButtonEnabled(false); this.findViewById(R.id.other_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, OtherActivity.class); startActivity(intent); } }); this.findViewById(R.id.external_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //調用圖片瀏覽器 Intent externalActivityIntent = new Intent(Intent.ACTION_PICK); externalActivityIntent.setType("image/*"); externalActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); startActivity(externalActivityIntent); } }); }}
4、進入到另一個Activity介面,OtherActivity.java:
package com.yangyu.myactionbar04;import android.app.ActionBar;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.NavUtils;import android.support.v4.app.TaskStackBuilder;import android.view.MenuItem;public class OtherActivity extends FragmentActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); final ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent upIntent = new Intent(this, MainActivity.class); if (NavUtils.shouldUpRecreateTask(this, upIntent)) { TaskStackBuilder.from(this) //如果這裡有很多原始的Activity,它們應該被添加在這裡 .addNextIntent(upIntent) .startActivities(); finish(); } else { NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); } }
源碼
【Android UI設計與開發】第12期:頂部標題列(三)ActionBar實現層級導航的返回效果