我的活动在打开时返回“Home”活动

时间:2013-10-25 03:01:24

标签: android android-actionbar

我创建了一个名为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活动,因为它什么都不做:(

1 个答案:

答案 0 :(得分:0)

当您设置导航回调时,它会立即触发,并且Home活动会启动,因为它会先行。例如,尝试设置标志初始化并在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;
}