6

I'd like to find out if it is possible to implement a navigation system where one child activity can have two parent activities. Basically, I have a stream of content which a user may favourite. They can share a saved item via email, from both the stream activity and an activity which displays "favourited" content. I want to avoid duplicating a class simply because of navigation.

4

1 回答 1

11

对的,这是可能的。但是如果有 2 个或更多的父母,你不能依赖向上导航的实现,如下所述:提供向上导航

所以,你有两个选择:

1-使用后退按钮行为

你可以通过调用finish()onBackPressed()在你onOptionsItemSelected(MenuItem item)android.R.id.home情况下做到这一点。像这样:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        return true;
}

2-返回到您的应用程序的第一个活动

让用户回到您的应用开始的第一个活动,如下所示:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        Intent backIntent = new Intent(this, YOUR_FIRST_ACTIVITY.class);
        backIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(backIntent);
        return true;
}

顺便说一句,这个问题可能是这个问题的重复

于 2013-11-22T13:12:00.393 回答