备份工具栏导航不会返回

时间:2016-05-20 07:20:06

标签: android

为什么备份工具栏导航无效?

在我正在设置工具栏

的活动中
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity);
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        usernam = (EditText) findViewById(R.id.username);
        passw = (EditText) findViewById(R.id.password);
        tvlogin = (TextView) findViewById(R.id.tvlogin);

    }
清单中的

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.je.www.i" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:name="com.je.www.i.GlobalContext"
        android:allowBackup="true"
        android:icon="@drawable/logomini"
        android:label="@string/app_name"
        android:theme="@style/MyMaterialTheme" >
        <activity android:uiOptions="splitActionBarWhenNarrow"
            android:name=".Activities.MainActivity"
            android:label="J" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="android.support.UI_OPTIONS"
                android:value="splitActionBarWhenNarrow"
                />

        </activity>
    <activity
        android:name=".Activities.LoginActivity"
        android:label="Login"
        android:parentActivityName=".Activities.MainActivity" >
    </activity>
</application>

2 个答案:

答案 0 :(得分:1)

您必须在处理向上导航的Activity中添加onOptionsItemSelected方法。

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

https://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp

答案 1 :(得分:1)

试试这个!!!

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loginactivity);
    toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });

    usernam = (EditText) findViewById(R.id.username);
    passw = (EditText) findViewById(R.id.password);
    tvlogin = (TextView) findViewById(R.id.tvlogin);

}