1

操作栏上的向上导航关闭 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>
4

3 回答 3

0

I have had the same issue previously. Try using the following code snippet. Credit goes to the link in the snippet:

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");
            }
于 2015-03-13T02:44:39.117 回答
0

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

    <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.AddressList" />
于 2015-03-13T12:38:08.930 回答
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);
    }

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

于 2018-06-22T11:27:19.527 回答