0

ListView を生成するランチャー アクティビティがあります。

ListView には、基本的に位置と ID を新しいアクティビティに渡す setOnItemClickListener があります。

mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(ActivityHome.this, ActivityDetail.class);
            intent.putExtra("cm_position", position);
            intent.putExtra("cm_id", id);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });

現在、ActivityHome の ListView は、ListView の途中に表示されています。

ActivityDetail にいて、actionBar.setDisplayHomeAsUpEnabled(true); がある場合。以下が実行されると、ActivityHomeに戻ります

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

問題は、ListView 内の位置が失われ、ListView が先頭に戻ることです。onCreate() の実行を停止し、ActivityDetail の Activity を破棄して、ActivityHome に戻って元の状態に戻す方法はありますか? ご理解いただければ幸いです。

インテントに対してフラグを間違って設定した可能性があると思います...

4

1 に答える 1

0

修理済み:

ActivityDetail では、このフラグを設定する必要があります

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

私はそれがフラグを適切に使用することと関係があることを知っていました!!!

上記の例で現在実行中のアクティビティ B のインスタンスは、ここで開始した新しいインテントを onNewIntent() メソッドで受け取るか、それ自体が終了して新しいインテントで再開されます。起動モードが「複数」(デフォルト) であると宣言されていて、同じインテントで FLAG_ACTIVITY_SINGLE_TOP を設定していない場合は、終了して再作成されます。他のすべての起動モードの場合、または FLAG_ACTIVITY_SINGLE_TOP が設定されている場合、このインテントは現在のインスタンスの onNewIntent() に配信されます。

于 2012-07-29T15:15:04.040 に答える