操作栏上的向上导航将关闭Android中的应用程序。非活动输入连接

时间:2015-03-13 01:58:42

标签: java android navigation android-actionbar up-navigation

操作栏上的向上导航将关闭Android中的应用。完全跟随android开发人员。我添加了.getSupportActionBar,.setDisplayHomeAsUpEnabled,夸大了我的菜单,将我的案例放在onCreateOptionsMenu中,但仍然无效。

public class AddressList extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_address_list);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ListView listview = (ListView) findViewById(R.id.listView);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_address_list, menu);
    return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.button:
            Add();
            return true;
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

这是我的清单文件,显示我已添加了父级名称和元数据。

  <activity
   android:name=".SilentGeofence"
        android:label="@string/title_activity_silent_geofence"
        android:parentActivityName=".AddressList" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".AddressList" />

    </activity>

3 个答案:

答案 0 :(得分:0)

我之前遇到过同样的问题。尝试使用以下代码段。 Credit转到代码段中的链接:

case android.R.id.home:

            //solution for the up navigation problem:
            //http://stackoverflow.com/questions/13632480/android-build-a-notification-taskstackbuilder-addparentstack-not-working
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                // This activity is NOT part of this app's task, so create a new task
                // when navigating up, with a synthesized back stack.
                TaskStackBuilder.create(this)
                        // Add all of this activity's parents to the back stack
                        .addNextIntentWithParentStack(upIntent)
                                // Navigate up to the closest parent
                        .startActivities();
            } else {
                // This activity is part of this app's task, so simply
                // navigate up to the logical parent activity.
                upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                NavUtils.navigateUpTo(this, upIntent);
                Log.v("recreate activity", "shouldUpRecreateTask = false");
            }

答案 1 :(得分:0)

在android:value中尝试使用父Activity的完整包名

    <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.AddressList" />

答案 2 :(得分:0)

如果仍然有人需要答案,这就是我的能力。

//First set Parent activity in your xml as shown above


<activity
       android:name=".ChildActivity"
            android:label="@string/title"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />

        </activity>

//Now you can simply get the parent Intent and use startActivity
@Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                Intent upIntent=NavUtils.getParentActivityIntent(this);
//You might need to add some Launch Parameters like
                upIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(upIntent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

您可以在此处找到有关这些启动参数的更多信息: Launch Parameter Info