0

在我的 android 应用程序中,我有一个搜索界面,可以单击每个列表项,从而启动一个新的 Activity,该 Activity 显示有关该单个项目的更多信息。我在新活动上有一个返回按钮,我想返回搜索结果列表。我尝试了一个可以返回搜索页面的意图,但这会开始一个新的搜索:

    Intent backIntent = new Intent(v.getContext(), SearchPage2.class); 
backIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivityForResult(backIntent, 0);
SearchTaDa.this.finish();

我也试过了SearchTaDa.this.finish();finish();但是这两个都关闭了整个应用程序。有人知道我如何通过单击“返回”按钮返回搜索结果吗?

4

4 回答 4

0

不要使用此代码,因为它会删除堆栈中的所有活动

backIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

请改用此代码:

Intent backIntent = new Intent(v.getContext(), SearchPage2.class); 
startActivityForResult(backIntent, 0);
SearchTaDa.this.finish();
于 2012-09-26T15:15:02.437 回答
0

只需执行此操作并尝试按 SearchPage2 上的返回按钮

Intent backIntent = new Intent(v.getContext(), SearchPage2.class); 
startActivityForResult(backIntent, 0);

不要清除堆栈,也不要在前一个上调用完成。

于 2012-09-26T15:17:11.857 回答
0

I think the problem is in your other Activity, from which you start the Detail Info Activity. Maybe you are calling finish() there after starting the new Activity, which removes it from the Stack, so you can't go back to it. You should then be able to use finish() in the second Activity to go back.

于 2012-09-26T15:20:46.617 回答
0

Basically, you want your button to emulate the devices's BACK button, because pressing devices's BACK button should return you to search results activity.

Not sure if it will work, but here's a trick:
In your "more info" activity override the onBackPressed() method which calls the super.onBackPressed(), then in the click listener of you back putton call: onBackPressed().

@Override
public void onBackPressed() {
    super.onBackPressed();
}

//.........
myBackButton.setOnClickListener(new OnClickListener(){
  public void onClick(){
    onBackPressed()
  }
});

And remove the setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);, when starting the "more info" activity.

于 2012-09-26T15:22:15.180 回答