向父级的导航始终是主要活动

时间:2014-03-14 10:50:36

标签: android navigation android-activity

我有活动A,我在其中启动了具有父活动C的活动C. 问题是,在活动C中触摸操作会将我带到我的MainActivity而不是Activity B.

    <activity
        android:name="com.ilikeq.android.BActivity"
        android:label="@string/title_activity_threads"
        android:screenOrientation="portrait"
        android:parentActivityName=".MainActivity"
        android:launchMode="singleTop">
    </activity>
    <activity
        android:name="com.ilikeq.android.CActivity"
        android:label="@string/title_activity_thread"
        android:screenOrientation="portrait"
        android:parentActivityName="com.ilikeq.android.BActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.ilikeq.android.BActivity" />
    </activity>

在活动C中:

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == android.R.id.home) {
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

活动B:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_threads);

        ListView list = (ListView) this.findViewById(R.id.list_threads);
        list.setAdapter(new ArrayAdapter<Thread>(this, R.layout.view_list_item_thread, new Thread[] { new Thread(), new Thread(), new Thread(), new Thread(), new Thread(), new Thread(), new Thread(), new Thread(), new Thread() }) {

            @Override
            public View getView(int position, View view, ViewGroup parent) {
                if (view == null) {
                    LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(R.layout.view_list_item_thread, null);
                }

                return view;
            }

        });

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                itemClicked();
            }
        });
    }

    public void itemClicked() {
        Intent intent = new Intent(this, ThreadActivity.class);
        this.startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        this.getMenuInflater().inflate(R.menu.threads, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        else if (id == android.R.id.home) {
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

活动A:

public void onMessageClick() {
    Intent intent = new Intent(this, ThreadsActivity.class);
    this.startActivity(intent);
}

它也会做错误的动画 - 显示新的儿童活动(显示效果)而非完成(消失效果)。

1 个答案:

答案 0 :(得分:0)

也许你需要清除一堆活动并开始一项新任务。这是通过向您的活动添加标志来实现的。 您可以在此处获得一些有用的见解 - http://developer.android.com/guide/components/tasks-and-back-stack.html

我认为这些对你来说很重要:

  • FLAG_ACTIVITY_CLEAR_TASK
  • FLAG_ACTIVITY_NEW_TASK

你也可以试试这个:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // chose to which activity to go to 
            Intent intent = new Intent(this, PreviousActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            return true;

    }
    return super.onOptionsItemSelected(item);
}

编辑:

在您的清单文件中,为父活动添加元标记:

<activity
        android:name="com.example.app.ActivityB"
        android:label="@string/title_activity_activity_b"
        android:parentActivityName=".MainActivity"
        android:screenOrientation="portrait">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="MainActivity" />
    </activity>
    <activity
        android:name="com.example.app.ActivityC"
        android:label="@string/title_activity_activity_c"
        android:screenOrientation="portrait"
        android:parentActivityName="com.example.app.ActivityB" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="ActivityB" />
    </activity>

此外,正如我前几天所怀疑的那样,你的MainActivity中有一个适配器 - 没有任何问题,但你使用它的方式效率不高。我建议你将它移出一个单独的文件并创建一个扩展ArrayAdapter的自定义适配器。然后,您可以使用ViewHolder模式对其进行优化 - 这样,即使您来回返回其他活动,也可以保持列表中的当前位置。 http://www.vogella.com/tutorials/AndroidListView/article.html https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView 查看ViewHolder部分。