2013-10-25 75 views
0

我創建了一個名爲Blocks的活動,但是當我打開它時,它會自動返回到Home活動,這是該活動的主要活動。我的活動在打開時返回到「主頁」活動

下面是代碼當我把代碼微調導航發生

Blocks.class 
public class Blocks extends FragmentActivity implements ActionBar.OnNavigationListener { 
    /** 
    * The serialization (saved instance state) Bundle key representing the 
    * current dropdown position. 
    */ 
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.blocks); 

      ImageView img_blocks = (ImageView) findViewById(R.id.map); 
      img_blocks.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        Intent myIntent = new Intent(Intent.ACTION_VIEW, 
         Uri.parse("geo:0,0?q=Rumaithiya+City")); 
        startActivity(myIntent); 
       } 
      }); 

     // Set up the action bar to show a dropdown list. 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setDisplayShowTitleEnabled(false); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 

     // Set up the dropdown list navigation in the action bar. 
     actionBar.setListNavigationCallbacks(
      // Specify a SpinnerAdapter to populate the dropdown list. 
      new ArrayAdapter<String>(
       actionBar.getThemedContext(), 
       android.R.layout.simple_list_item_1, 
       android.R.id.text1, 
       new String[] { 
        getString(R.string.title_section1), 
        getString(R.string.title_section2), 
        getString(R.string.title_section3), 
        getString(R.string.title_section4), 
        getString(R.string.title_section5), 
       }), 
      this); 
    } 

    @Override 
    public void onRestoreInstanceState(Bundle savedInstanceState) { 
     // Restore the previously serialized current dropdown position. 
     if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { 
      getActionBar().setSelectedNavigationItem(
       savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); 
     } 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     // Serialize the current dropdown position. 
     outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, 
      getActionBar().getSelectedNavigationIndex()); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.blocks, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.menumap: 
       Intent myIntent = new Intent(Intent.ACTION_VIEW, 
        Uri.parse("geo:29.316097,48.071147?z=14")); 
       startActivity(myIntent); 
       break; 
      case R.id.menuwiki: 
       String wiki = "http://j.mp/rumwiki"; 
       Intent w = new Intent(Intent.ACTION_VIEW); 
       w.setData(Uri.parse(wiki)); 
       startActivity(w); 
       break; 
      default: 
       break; 
     } 
     return true; 
    } 

    @Override 
    public boolean onNavigationItemSelected(int position, long id) { 
     // When the given dropdown item is selected, show its contents in the 
     // container view. 
     switch (position) { 
      case 0: 
       startActivity(new Intent(this, Home.class)); 
       return true; 
      case 1: 
       startActivity(new Intent(this, Landmarks.class)); 
       return true; 
      case 2: 
       startActivity(new Intent(this, Blocks.class)); 
       return true; 
      case 3: 
       startActivity(new Intent(this, Numbers.class)); 
       return true; 

      default: 
       break; 
     } 
    return true; 
    } 
} 

的問題,我可以回去「塊」只需按後退按鈕(它會不會去「又到家了)。

編輯:這是按要求

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.rum.city" 
android:versionCode="10" 
android:versionName="3.0" > 

<uses-sdk 
    android:minSdkVersion="15" 
    android:targetSdkVersion="15" /> 

<uses-permission android:name="android.permission.INTERNET" /> 

<uses-feature 
    android:name="android.hardware.telephony" 
    android:required="false" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.rum.city.Home" 
     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.rum.city.Landmarks" /> 
    <activity 
     android:name="com.rum.city.Blocks" 
     android:label="@string/title_activity_blocks" > 
    </activity> 
    <activity 
     android:name="com.rum.city.Numbers" 
     android:label="@string/title_activity_numbers" > 
    </activity> 
    <activity 
     android:name="com.rum.city.Hwl" 
     android:label="@string/title_activity_hwl" 
     android:theme="@android:style/Theme.Holo.Light.Dialog" > 
    </activity> 

</application> 

編輯我的清單代碼:由於其他用戶稱,savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)返回0,可以肯定,我改變了我的代碼:

switch (position) { 
      case 0: 
       return true; 

而程序沒有去Home,但問題是這樣用戶將無法使用微調器來選擇Home活動,因爲它什麼都不會做:(

+0

您正在使用哪個API版本? – user463226

+0

感謝您的評論,我正在使用API​​ 15(ICS 4.0.3) – Cyclone

+0

您能否顯示您的清單文件? –

回答

0

當您設置導航回調時,它會立即觸發並且首頁活動因爲它首先進行而啓動。例如嘗試設置標誌初始化並在onNavigationItemSelected中檢查它。

private boolean mInitialized = false; 

@Override 
public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    actionBar.setListNavigationCallbacks(adapter, 
       this); 
    mInitialized = true;    
} 

@Override 
    public boolean onNavigationItemSelected(int position, long id) { 
     if (!mInitialized) { 
      return false; 
     } 

     return true; 
} 
相關問題